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

93 questions
46
votes
5 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
  • 509
  • 1
  • 4
  • 6
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
  • 23,776
  • 22
  • 102
  • 222
17
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
  • 5,726
  • 1
  • 26
  • 51
16
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
  • 2,728
  • 2
  • 24
  • 58
15
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
  • 529
  • 1
  • 5
  • 12
15
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
  • 553
  • 2
  • 11
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
  • 50,103
  • 11
  • 109
  • 162
13
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
  • 510
  • 1
  • 5
  • 17
12
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,749
  • 1
  • 16
  • 29
11
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
  • 58,757
  • 13
  • 108
  • 182
11
votes
1 answer

constexpr std::optional reset

I was reviewing the interface for the C++-17 std::optional class template and noticed that the reset and assignment from nullopt are not marked as constexpr. Was this an oversight or is there a reason that this operation cannot be marked constexpr?
apmccartney
  • 707
  • 8
  • 14
10
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,457
  • 1
  • 13
  • 26
9
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
  • 50,257
  • 51
  • 171
  • 339
8
votes
1 answer

Does anything prevent std::optional::value_or() from being conditionally noexcept?

Here's the definition of value_or() from the C++17 standard: template constexpr T value_or(U&& v) const&; Effects: Equivalent to: return bool(*this) ? **this : static_cast(std::forward(v)); Remarks: If is_copy_constructible_v &&…
knatten
  • 4,906
  • 3
  • 18
  • 26
7
votes
1 answer

Why does the const rvalue qualified std::optional::value() return a const rvalue reference?

std::optional::value() has the following two overloads constexpr T& value() &; constexpr const T & value() const &; constexpr T&& value() &&; constexpr const T&& value() const &&; What is the point of returning a const rvalue reference? The only…
Curious
  • 19,352
  • 6
  • 45
  • 114
1
2 3 4 5 6 7