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
7
votes
2 answers

Does passing an std::optional by reference actually save copying?

I know that std::optional isn't supported in the standard. This question is about whether passing std::optional& has any performance advantage Sample code (https://godbolt.org/z/h56Pj6d6z) reproduced here #include #include…
resnet
  • 398
  • 3
  • 12
7
votes
1 answer

How can I use std::optional to return a NULL value?

How can I return a value, similar to null, with std::optional? My function receives an int index as parameter to iterate through a list, if the index value is not valid, how this std::optional can be used to return a value similar to null?
Porton_
  • 113
  • 1
  • 5
7
votes
0 answers

Shouldn't std::optional contain its value while the emplacing constructor is executing?

In real code, constructors can be very complex and so the emplace of a std::optional can query the status of the optional itself. When this happens it's usually a bit more convoluted, but here is a (contrived) minimal example: #include…
Tobi
  • 2,591
  • 15
  • 34
7
votes
1 answer

Is '_HAS_CXX17' macro usable in custom project headers to enable C++17 language set features?

I want to create headers which use 'optional' from standard C++. However, My headers will be referred from Visual Studio 2015 as well as Visual Studio 2017 projects. I would like to have something, such that for Visual Studio 2017 ( with C++ 17 lang…
Ishita
  • 489
  • 5
  • 15
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
  • 20,870
  • 8
  • 61
  • 146
6
votes
3 answers

When would you use optional>

I was reading References, simply and got to the part talking about using optional references. One of the reasons Herb gives to avoid optional is because those situations can be "represented about equally well by optional>" I'm…
janovak
  • 1,531
  • 2
  • 12
  • 22
6
votes
2 answers

How to move a value out of a std:optional without calling the destructor?

I am trying to write a function, make_foo, that will "unwrap" a std::optional< foo >, returning the contained value. The function assumes that the optional is engaged so does not perform any runtime checks on the optional. My implementation of this…
jonnybolton
  • 487
  • 4
  • 7
5
votes
1 answer

Why doesn't iterate over a container accessed directly through std::optional::value() work?

I'm trying to iterate over a std::vector contained in a struct T that I access through a std::optional. Contrary to my expectations, the behavior is different if I first store the std::optional into a copy versus if I iterate it…
w128
  • 4,680
  • 7
  • 42
  • 65
5
votes
1 answer

Does std::optional contain a value during construction?

Check out the following code for an example: #include #include class A { public: A(); ~A(); }; std::optional a; A::A() { std::cout << a.has_value(); } A::~A() { std::cout << a.has_value(); } int main() { …
Paul
  • 6,061
  • 6
  • 39
  • 70
5
votes
2 answers

What is the use case for not having lazy evaluation of value_or()?

I was having trouble when using foo.value_or(bar()) in my code, because I wasn't expecting the function bar() to be called when the optional variable foo had a value. I've since found this question that explains that value_or() doesn't use lazy…
5
votes
1 answer

Should I move the value out of an optional or move the whole optional?

Is there an effective difference between std::move(*optional) and *std::move(optional)? Which one is preferable? Full example: #include #include void foo() { std::optional> ov = std::vector{}; …
SteakOverflow
  • 1,953
  • 13
  • 26
5
votes
1 answer

In which cases does std::optional operator == cause undefined behavior?

Cppreference has the following description of mixed (optional and some other non-optional type) comparison operators for std::optional: Compares opt with a value. The values are compared (using the corresponding operator of T) only if opt contains…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
5
votes
1 answer

std::optional default constructor not being constexpr in gcc?

I have the following code to test my constexpr-constructible lazy class: https://godbolt.org/z/rMLCiL #include template class Lazy { using initializer_t = T (*)(); std::optional m_val = std::nullopt; …
eivour
  • 1,678
  • 12
  • 20
5
votes
4 answers

Am I abusing std::optional

How std::optional fits in my code? My code contains MANY functions that looks roughly like: bool GetName(_Out_ string& strRes) { // try to get/calculate the value // value may also be empty // return true on success, false otherwise. } Now…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
5
votes
1 answer

When is it appropriate to use std::optional

I was wondering if this would be considered a valid usage of std::optional. I have a function that returns a process_id (std::uint32_t value), would it be more efficient to have a standard "std::uint32_t" function that returns 0 if we fail to find…
devptr
  • 51
  • 1
  • 4
1 2
3
11 12