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

How do I return an optional pointer or reference ( std::optional )?

Suppose I have the following template function: template std::optional> larger(const T data[], size_t count) { if(!count) return std::nullopt; size_t index_max {}; for(size_t i {1ULL}; i <…
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
2
votes
1 answer

Disable std::optional's forwarding constructor

I've extended QDataStream with a template conversion operator so that the datastream loads from itself and converts to any supported type, as such: class ConvertibleQDataStream : public QDataStream { public: using QDataStream::QDataStream; …
Unimportant
  • 2,076
  • 14
  • 19
2
votes
3 answers

Why there is no throw or sigsegv while accessing empty std::optional?

The example: #include #include using namespace std; int main() { optional t{}; // nullopt (empty) by default cout << *t << endl; return 0; } Actually this program prints some int (uninitialized value of…
vladon
  • 8,158
  • 2
  • 47
  • 91
2
votes
1 answer

use of std::experimental::optional with g++ v8.2.0

What was once known as std::experimental::optional is now known as std::optional in C++17. But some libraries -- such as libpqxx -- haven't yet been updated to drop the experimental namespace. So now with the new version of Ubuntu 18.10 which comes…
Stéphane
  • 19,459
  • 24
  • 95
  • 136
2
votes
4 answers

What's an idiomatic way to refer to either std::whatever or not_yet_in_std::whatever?

I like spans, so I use gsl::span here and there. But - in C++20, it's going to be std::span instead*. I use std::optional, but for C++14 code, it needs to be std::experimental::optional. And so on. What's an idiomatic and sort-of future-proof way to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
3 answers

C++17: using std::optional to evaluate if enum contains value

I'd like to check at compile-time if various enums contain a given value, so I've written the following: #include enum class test_enum : int { VALUE_0 = 0, VALUE_1 = 1 }; // Template function to perform check template
tstevens
  • 63
  • 8
2
votes
0 answers

Any way to move constexpr function that returns std::optional inside struct where it is used?

I understand that the title of the question is confusing, so here is the example: #include #include #include static constexpr std::optional maybe_int() { return {64}; } struct s { using type =…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
2
votes
4 answers

Alternative to value_or that is indifferent to the returned type

I have built a little class Card overloading the << operator, essentially printing out Suit and value of the card. The implementation details are not relevant for the question I want to ask here just assume the obvious. For Card I built a class…
2
votes
1 answer

Does clang6 implement std::optional?

I want to use the C++17 std::optional but it seems to be absent in clang: > cat test.cxx #include int main(int, char **) { return 0; } > $CXX --version | head -n1 clang version 6.0.0 (trunk 317775) > $CXX -std=c++17 test.cxx…
avitase
  • 134
  • 1
  • 8
2
votes
2 answers

Do std::optional and boost::optional respect alignment restrictions of the managed object?

If a class T has an alignment requirement, such as one specified by the alignas keyword, are std::optional and boost::optional guaranteed to respect said alignment? If they were simply wrapper classes for a T object and a bool initialized,…
patatahooligan
  • 3,111
  • 1
  • 18
  • 27
1
vote
1 answer

Pass `std::optional::value` to `std::views::transform`

I cannot seem to pass std::optional::value to std::views::transform. However, I can pass std::optional::has_value without any issues: #include #include #include int main () { std::vector>…
Antonio
  • 355
  • 1
  • 2
  • 13
1
vote
3 answers

Interaction between std::optional and has_value()

For debugging purposes, I was writing a function which iterates over a vector of optional variables of any type to check which ones were initialized, but the check for has_value() on all of them is returning true, despite no value having ever been…
1
vote
2 answers

std::minmax_element comparer for a vector with std::optional is incorrect

I have a struct within a struct of std::optionals, and I am trying to find the minimum and maximum value within the struct. However, using std::minmax_element seems to be inaccurate, and I've had to split the comparison function and use…
Brinck
  • 69
  • 4
1
vote
1 answer

Can I make std::optional::value() return the (polymorphic) inherited exception, and not std::exception?

Apparently, throwing and catching std::optional::value() cannot be handled polymorphically. First if we consider try { throw std::logic_error("blah .."); } catch(const std::exception& e) { std::cout << "E: " << e.what() <<…
Wololo
  • 1,249
  • 1
  • 13
  • 25