Questions tagged [boost-optional]

A Boost C++ library that provides a container that can represent uninitialized objects of arbitrary type, notably allowing easier definition of functions that might not have a value to return

100 questions
174
votes
4 answers

How should one use std::optional?

I'm reading the documentation of std::experimental::optional and I have a good idea about what it does, but I don't understand when I should use it or how I should use it. The site doesn't contain any examples as of yet which leaves it harder for me…
David G
  • 94,763
  • 41
  • 167
  • 253
58
votes
2 answers

How to set a boost::optional back to an uninitialized state?

How can I "reset"/"unset" a boost::optional? optional x; if( x ) { // We won't hit this since x is uninitialized } x = 3; if( x ) { // Now we will hit this since x has been initialized } // What should I do here to bring x back to…
Guy Sirton
  • 8,331
  • 2
  • 26
  • 36
50
votes
6 answers

std::optional specialization for reference types

Why std::optional (std::experimental::optional in libc++ at the moment) does not have specialization for reference types (compared with boost::optional)? I think it would be very useful option. Is there some object with reference to maybe already…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
42
votes
4 answers

How to use boost::optional

I am trying to use boost::optional as below. #include #include #include struct myClass { int myInt; void setInt(int input) { myInt = input; } int getInt(){return myInt;…
polapts
  • 5,493
  • 10
  • 37
  • 49
37
votes
4 answers

How to get around GCC ‘*((void*)& b +4)’ may be used uninitialized in this function warning while using boost::optional

I have code similar to the following: #include ::boost::optional getitem(); int go(int nr) { boost::optional a = getitem(); boost::optional b; if (nr > 0) b = nr; if (a != b) return 1; …
Paul Omta
  • 1,703
  • 1
  • 12
  • 17
34
votes
2 answers

How to move from std::optional

Consider the following example where we parse data and pass the result to the next function: Content Parse(const std::string& data); void Process(Content content); int main() { auto data = ReadData(); Process(Parse(data)); } Now let's…
hansmaad
  • 18,417
  • 9
  • 53
  • 94
27
votes
3 answers

retrieving an object from boost::optional

Suppose a method returns something like this boost::optional SomeMethod() {...} Now suppose I have something like this boost::optional val = SomeMethod(); Now my question is how can I extract SomeClass out of val ? So that I…
MistyD
  • 16,373
  • 40
  • 138
  • 240
27
votes
1 answer

What is the rationale for boost::none_t implementation?

Boost.Optional uses a dummy type to allow constructing uninitialized instances of boost::optional. This type is called none_t, and an instance none is already defined in a header for convenience, allowing us to write code such as the…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
26
votes
3 answers

Is it possible to move a boost::optional?

I've been trying to define a defaulted move constructor in a class with a boost::optional member variable. #include #include #include struct bar {std::vector vec;}; struct foo { foo() = default; …
user1203803
23
votes
7 answers

When to use boost::optional and when to use std::unique_ptr in cases when you want to implement a function that can return "nothing"?

From what I understand there are 2* ways you can implement a function that sometimes doesnt return a result(for example is person found in a list of ppl). *- we ignore raw ptr version, pair with a bool flag, and exception when none found…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
22
votes
3 answers

boost::optional alternative in C++ Standard Library

I'm trying to get my program working without boost usage, but can't find an alternative of some useful patterns. Namely, I can't find boost::optional-likewise pattern in the standard library. Is there some standard alternative for boost::optional…
Dmitry Bespalov
  • 5,179
  • 3
  • 26
  • 33
18
votes
2 answers

boost::optional vs T*

I'm trying to understand when is the right time to use some of the structures that come with boost and had a question regarding the use of boost::optional with a reference. Suppose I have the following class, using boost::optional: class MyClass…
Lee
  • 303
  • 3
  • 6
17
votes
2 answers

Calling Functions With std::optional Parameters

I have a function whose signature is: void func(std::optional os = std::nullopt); (I’m aliasing std::experimental::optional until std::optional is officially available.) However, I’m having difficulty calling it cleanly. The compiler…
ThatsJustCheesy
  • 1,370
  • 14
  • 24
15
votes
3 answers

Conversion of boost::optional to bool

How I can prevent the last line of this code from compiling? #include int main() { typedef boost::optional int_opt; int_opt opt = 0; bool x = opt; // <- I do not want this to compile } The last line doesn't…
dimba
  • 26,717
  • 34
  • 141
  • 196
14
votes
2 answers

Is using std::optional as efficient as using int?

I have a quad-/octree data structure. Im storing the children indexes/ptrs of a cell in an array. Each position in the array represents the location of a child with respect to its parent, e.g. in 2D: // _____________ // | | | // | 2 | 3 …
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
1
2 3 4 5 6 7