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
12
votes
1 answer

How to efficiently return an std::optional

I would like to ask how to return an std::optional in an efficient way and I would like to use std::make_optional(). For example lets have this code snippet: std::optional CreateCanonicalPath(const std::string_view& path) { std::error_code…
Jiří Lechner
  • 750
  • 6
  • 19
12
votes
3 answers

What are monadic bind and monadic return for C++23 optional?

C++23 std::optional is finally getting some very useful additions. Since my knowledge of FP is very primitive I am wondering what is the syntax for the following two operations(that according to my googling are 2 basic monadic operations): monadic…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
12
votes
1 answer

namespace std:: does not contain optional

i am doing the Vulkan Tutorial https://vulkan-tutorial.com/ #define GLFW_INCLUE_VULKAN #include #include struct s { std::optional num;//Intellisense Error }; int main() { return 5; } I started with an…
DuckPuppy
  • 123
  • 1
  • 1
  • 7
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
  • 743
  • 8
  • 16
11
votes
5 answers

What's the advantage of `std::optional` over `std::shared_ptr` and `std::unique_ptr`?

The reasoning of std::optional is made by saying that it may or may not contain a value. Hence, it saves us the effort of constructing a, probably, big object, if we don't need it. For example, a factory here, will not construt the object if some…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
10
votes
3 answers

What's the difference between C++23's optional::transform and optional::and_then?

C++23 adds some "monadic-style" functionality regarding optionals, as methods of optional: optional::and_then() (and ignoring qualifiers of this): template constexpr auto and_then(F&& f); Returns the result of invocation of f on…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
10
votes
2 answers

Take value out of std::optional

How do you actually take a value out of optional? Meaning take ownership of the value inside the std::optional and replace it with std::nullopt (or swap it with another value)? In Rust for example you could .unwrap your Option or do something like…
Silver
  • 1,327
  • 12
  • 24
10
votes
5 answers

How best to test and unwrap std::optional in an if statement

I have multiple functions that return a std::optional. Here's an example for a made-up type MyType: struct MyType { // ... } std::optional calculateOptional() { // ... lengthy calculation if (success) { return…
Alex
  • 5,009
  • 3
  • 39
  • 73
10
votes
2 answers

Use std::optional as a regular pointer vs use has_value() and value

std::optional can use the syntax to access its value similar to a normal pointer, like . std::optional some_str; if (some_str) (*some_str).c_str(); but it also has two functions, has_value() and value() to provide access to its value…
Gordon Tseng
  • 171
  • 1
  • 5
9
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
  • 5,191
  • 3
  • 22
  • 31
8
votes
1 answer

How to in-place-construct an optional aggregate?

How to in-place construct an optional aggregate? It seems I can only construct an optional single thing, and not an optional aggregate of things. #include #include struct Unmovable { Unmovable(const Unmovable&) = delete; …
user2394284
  • 5,520
  • 4
  • 32
  • 38
8
votes
3 answers

In C++17 can an if statement with an initializer be used to unpack an optional?

I'm writing some code using std::optional's and am wondering if C++17's 'if statements with initializers' will be able to help unpack values? std::optional optionalInt = GetOptionalInt(); I'm making up the function Unpack here: if( auto…
Scott Langham
  • 58,735
  • 39
  • 131
  • 204
7
votes
2 answers

Why is there no built-in way to get a pointer from an std::optional?

Things like the following happen all to often when using std::optional: void bar(const Foo*); void baz(const std::optional& foo) { // This is clunky to do every time. bar(foo.has_value() ? &foo.value() : nullptr); // Why can't…
Matt
  • 21,026
  • 18
  • 63
  • 115
7
votes
0 answers

MSVC generating unnecessary complicated instructions

While benchmarking code involving std::optional, I noticed that the code MSVC generates runs at roughly half the speed compared to the one produced by clang or gcc. After spending some time reducing the code, I noticed that MSVC apparently…
Sedenion
  • 5,421
  • 2
  • 14
  • 42
7
votes
1 answer

How to access contained value in C++23 std::optional without writing identity boilerplate?

I am playing with C++23 std::optional additions, but I can not figure out how to elegantly access the value of object if optional is active. I know I can use if, but that is so C++20. I really like C++23 API changes, but I can not figure out how to…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
2
3
11 12