Skip to main content

All Questions

Tagged with
-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::...
o_o's user avatar
  • 1
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 ...
einpoklum's user avatar
  • 127k
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 ...
Silver's user avatar
  • 1,408
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 ...
Ionut Alexandru's user avatar
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:...
Enlico's user avatar
  • 26.7k
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::...
Enlico's user avatar
  • 26.7k
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 ...
Enlico's user avatar
  • 26.7k
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: ...
Rasmus Källqvist's user avatar
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 ...
Aviv Cohn's user avatar
  • 16.7k
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 ...
passing_through's user avatar
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}, ...
Eugene's user avatar
  • 11.3k
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/...
Ton van den Heuvel's user avatar
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 ...
Drew Dormann's user avatar
  • 61.7k
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>;...
r3mus n0x's user avatar
  • 6,104
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 ...
Caesar's user avatar
  • 971

15 30 50 per page