Questions tagged [stdoptional]

In C++ the std::optional class template represents an optional value, i.e. one that may or may not be present.

The C++ class template std::optional<T> represents an optional value of type T, so that either it contains a valid value of type T or it contains nothing.

Related tags

173 questions
1
vote
3 answers

std::optional appears to destroy T twice when leaving its scope. Why?

I'm looking to replace some old raw pointer handling for objects (unparented QObjects) that in practice are treated as optional objects. E.g: if(m_file) delete m_file; m_file = new QFile(...);` Both std::unique_ptr and std::optional seem to be…
1
vote
1 answer

Why does this coroutine implementation for std::optional not work?

I took the implementation of coroutines for std::future from the documentation at https://en.cppreference.com/w/cpp/coroutine/coroutine_traits and modified it for std::optional #include #include #include template…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1
vote
1 answer

Why do the C++ Core Guidelines not recommend to use std::optional over pointers when approriate?

In the C++ Core Guidelines std::optional is only referred once: If you need the notion of an optional value, use a pointer, std::optional, or a special value used to denote “no value.” Other than that, it is not mentioned in the guidelines, so in…
Henk
  • 826
  • 3
  • 14
1
vote
2 answers

Can derived classes call base class constructor with optional parameters?

If I have a base class with a constructor : class Base{ Base(std::optional a, std::optional b, std::optional c, std::optional d) : _a(a), _b(b), _c(c), _d(d) { } private: std::optional _a; …
callum arul
  • 159
  • 6
1
vote
1 answer

Iterating over std::optional

I tried to iterate over an std::optional: for (auto x : optionalValue) { ... } With the expectation of it doing nothing if optionalValue is empty but doing one iteration if there is a value within, like it would work in Haskell (which arguably…
yairchu
  • 23,680
  • 7
  • 69
  • 109
1
vote
1 answer

Assigning values to std::array of std::optional objects

I am trying to fill a std::array of std::optional objects as below. class MyClass { private: int distance; MyClass(int x, int y); friend class MasterClass; }; MyClass::MyClass(int x, int y) { distance = x+y; } class MasterClass { …
1
vote
1 answer

hold reference object inside std::optional

I am having issue when trying to pass a reference object inside std::optional as a function argument.Does it not support storing reference objects ? Example - void fun(std::optional) What exatly does this error mean - static_assert…
code4fun
  • 2,661
  • 9
  • 25
  • 39
1
vote
1 answer

Combining auto template parameters with std::optional, possible?

I really like C++17's auto template parameters as I don't have to jump through hoops in order to use non-type template arguments (such as functions with forwarded arguments). But it got me thinking if it was possible to combine it with some other…
Nobilis
  • 7,310
  • 1
  • 33
  • 67
1
vote
1 answer

Math on std::optionals?

When I try this: #include using namespace std; int main() { return make_optional(2) +…
sprajagopal
  • 322
  • 1
  • 4
  • 20
1
vote
1 answer

Using boost::program_options with std::optional

Boost's program_options library now supports boost::optional, can the same be done with std::optional? I attempted to modify both the documentation example and the code in the PR, but neither seems to work. For example, the very simple case for…
Felix
  • 2,064
  • 3
  • 21
  • 31
1
vote
1 answer

std::optional does not compile

This code does not compile with the command g++ -std=c++17 main.cpp #include #include int main() { std::optional x; std::cout << "Hello World"; return 0; } The Errors are the following: error:…
EnnFour
  • 323
  • 2
  • 8
1
vote
0 answers

Best way to represent a array of std::optional in protobuf

I need to map my "native" C++ struct that has an array(std::array, but I presume for std::vector solution is same) to protobuf. So let' say I have struct MyStruct{ // Color is some class I already know how to map to PB …
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

How does std::optional delay initialization? / How is std::optional implemented?

Lately I've taken interest in initialization. One of the things I'm particularly interested in, is std::optional for its ability to initialize an instance of a type after it's been declared. I have tried reading the code inside the optional header,…
1
vote
1 answer

Is std::optional set to std::nullopt when it is declared as a local variable and not explicitly initialized?

When an int is declared locally (but not initialized or assigned to), it is of undefined value. When std::optional is declared locally without an explicit initialization, does the same apply? Is it always std::nullopt, or is it of undefined…
jhourback
  • 4,381
  • 4
  • 26
  • 31
1
vote
1 answer

Undefined reference with std::experimental::optional despite symbol being present

After fiddling with a bunch of CMake settings in a project I'm working on, I'm encountering a linking issue which I didn't previously experience. In a nutshell, I have a static library (.a file) with the following symbol…
einpoklum
  • 118,144
  • 57
  • 340
  • 684