All Questions
Tagged with option-type c++17
59
questions
-4
votes
0
answers
75
views
std::optional is not available though I'm using G++9 [closed]
`error: ‘optional’ in namespace ‘std’ does not name a template type
** | std::optional<double> ;
| ^~~~~~~~
‘std::optional’ is only available from C++17 onwards
** | std::...
1
vote
8
answers
138
views
Decent idiom for initializing an optional to either null or a value?
Motivation
With C++ having gotten optional's (in C++17), it is now common to want to write the equivalent of:
If a condition holds, initialize my variable with some expression; and if the condition ...
16
votes
2
answers
15k
views
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 ...
1
vote
1
answer
385
views
Template argument deduction failed, trying with std::variant
I have the following program similar with my problem. I need to get the specific class from a method like getClass and after that pass the object and call similar defined method. I can't use ...
4
votes
1
answer
185
views
If-then-else vs ternary operator when returning full or empty std::optional
(I've not found much by searching for return statement, return deduce, and similar, with tags c++optional.)
Why does this work
#include <optional>
auto const f = [](bool b) {
return b ? std:...
0
votes
2
answers
442
views
How do I obtain a non-empty optional with the value inside it being default constructed?
Let's say I have a default constructed, thus empty, object ov of type std::optional<std::vector<int>>.
Yeah, std::vector can express the concept of being empty without the help of std::...
1
vote
0
answers
62
views
If std::optional's bool conversion operator is explicit, why can we use an optional as a condition for an if statement? [duplicate]
Probably this means that I have misunderstood the meaning of explicit in general, but with specific example below, what allows a std::optional to be used as a conditon of an if, if its conversion ...
4
votes
2
answers
525
views
Why doesn't make_optional work for file streams?
I'm trying out the C++17 optional-type, and thought a fitting place to use it would be a function that attempts to open a file and maybe returns the opened file. The function I wrote looks like this:
...
3
votes
3
answers
2k
views
optional<reference_wrapper<T>> vs. optional<T>& - practical examples?
I have read about std::optional<std::reference_wrapper<T>> as a way to pass around optional references.
However, I'm unable to think of a practical example where I'd do that, instead of ...
1
vote
0
answers
157
views
Why isn't std::optional<T>::value() a free function?
As I know, it is recommended to use non-friend non-member functions when possible as it decouples algorithms working with the class from its private fields. Using this logic, we would probably ...
36
votes
2
answers
4k
views
How to initialize C++17 vector of pairs with optional element
In C++17, how do you declare and initialize a vector of pairs(or tuples) with an optional element?
std::vector<std::pair<int, optional<bool> > > vec1 = { {1, true},
...
4
votes
1
answer
91
views
Class template argument deduction for template type which is itself an argument for a template
Is class template argument deduction supported for template types with default arguments which are used within another template type declaration? The following code does not compile with both Clang/...
73
votes
4
answers
43k
views
std::optional - construct empty with {} or std::nullopt?
I thought that initializing a std::optional with std::nullopt would be the same as default construction.
They are described as equivalent at cppreference, as form (1)
However, both Clang and GCC seem ...
29
votes
1
answer
2k
views
Why is there a dummy union member in some implementations of std::optional?
Both libstdc++ (GNU) and libc++ (LLVM) implement std::optional value storage using a union and both of them include a dummy member.
GNU implementation:
using _Stored_type = remove_const_t<_Tp>;...
1
vote
1
answer
138
views
c++ optional(T) -> optional<T> [duplicate]
I am reading std::optional in the page 539 of the draft n4791.
I notice that there is a
template<class T>
optional(T) -> optional<T>;
What is the meaning of this statement?
How does ...