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
58
votes
6 answers

Is it possible to set an object to null?

Further in my code, I check to see check if an object is null/empty. Is there a way to set an object to null?
John
  • 629
  • 1
  • 5
  • 7
27
votes
3 answers

Can I copy-elide an immovable & uncopyable function result into an optional?

I want to store a non-trivial type that is immovable and not copyable in an std::optional. However the object is constructed by a free function. (example) struct Foo { Foo(); Foo(Foo const&) = delete; Foo(Foo&&) = delete; Foo&…
bitmask
  • 32,434
  • 14
  • 99
  • 159
27
votes
2 answers

What is the point of `std::make_optional`

All the std::make_ are made redundant by C++17 with the introduction of Class template argument deduction (except make_unique and make_shared). So what is the point of std::make_optional? As far as I can tell it does the exact same thing as the…
bolov
  • 72,283
  • 15
  • 145
  • 224
22
votes
1 answer

Why do std::optional constructors use std::in_place?

Some std::optional constructors use an std::in_place_t tag parameter like this: template< class... Args > explicit optional( std::in_place_t, Args&&... args ); I see that such constructors could be implemented without the in-place tag and use some…
oliora
  • 839
  • 5
  • 12
20
votes
3 answers

Why does std::optional::value_or() take a U&& rather than T&&?

On cppreference, we can see std::optional takes a default value of U&& rather than T&&. It makes me incapable of writing the following code: std::optional> opt; opt.value_or({1, 2}); // does not…
calvin
  • 2,125
  • 2
  • 21
  • 38
20
votes
3 answers

std::optional::value_or() - lazy argument evaluation

Is it possible to evaluate std::optional::value_or(expr) argument in a lazy way, so the expr were calculated only in the case of having no value? If not, what would be a proper replacement?
vtrz
  • 591
  • 1
  • 6
  • 12
19
votes
3 answers

How to flatten the nested std::optional?

note: this question was briefly marked as a duplicate of this, but it is not an exact duplicate since I am asking about std::optionals specifically. Still a good question to read if you care about general case. Assume I have nested optionals,…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
19
votes
2 answers

clang 5: std::optional instantiation screws std::is_constructible trait of the parameter type

A really strange and unexpected behaviour of clang 5 was detected when switching to c++17 and replacing custom std::optional solution with the standard one. For some reason, emplace() was being disabled due to faulty evaluation of a…
GreenScape
  • 7,191
  • 2
  • 34
  • 64
19
votes
1 answer

How do I use std::optional in C++?

I am trying to use std::optional but my code raise error. I have specified #include and compiler options are -std=c++1z, -lc++experimental. How to use std::experimental::optional? The following is code: #include…
KiYugadgeter
  • 3,796
  • 7
  • 34
  • 74
16
votes
2 answers

std::optional implemented as union vs char[]/aligned_storage

While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template class optional { // ... private: bool has_value_; aligned_storage
Ron
  • 1,989
  • 2
  • 17
  • 33
16
votes
2 answers

std optional: No such file or directory

I tried to compile the following program with different compilers (including gcc 6.1) : #include int main() { std::optional o1; } Output is main.cpp:1:20: fatal error: optional: No such file or directory #include optional This…
scoulomb
  • 630
  • 2
  • 7
  • 19
16
votes
2 answers

Overhead of std::optional?

Now that std::experimental::optional has been accepted (or is about to be accepted), I wonder what is the overhead and the consequences on the assembly generated when the inner value is get by the following operators : -> * value value_or compared…
Vincent
  • 57,703
  • 61
  • 205
  • 388
15
votes
3 answers

Is there any advantage in using std::optional to avoid default arguments in a function?

I'm porting code to C++17, trying to use the new features while possible. One thing I like is the idea to use std::optional to return or not a value in a function that may fail in some conditions. I was curious about the possible usages of this new…
mohabouje
  • 3,867
  • 2
  • 14
  • 28
14
votes
3 answers

How is std::optional never "valueless by exception"?

std::variant can enter a state called "valueless by exception". As I understand, the common cause of this is if a move assignment throws an exception. The variant's old value isn't guaranteed to be present anymore, and neither is the intended new…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
13
votes
1 answer

Why is a std::move of a value from a const std::optional possible?

Why is the following snippet legal C++ code? This is a purely theoretical question - there is no usecase that I have in mind: #include #include #include int main() { std::vector v{1, 2, 3}; const…
Markus Moll
  • 163
  • 8
1
2 3
11 12