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

What's the point of ref-qualified member functions in `std::optional::value`

These are the signatures, according to Cppreference: constexpr T& value() &; constexpr const T& value() const &; constexpr T&& value() &&; constexpr const T&& value() const &&; What's the point of using &/const& and &&/const&&? Especially, I don't…
BIuesky
  • 97
  • 5
2
votes
3 answers

Using std::optional to invalidate my RAII object in move constructor/asignment

Say I have an RAII class, instances of which should never be copied: class Session { public: Session(); // Allocates a resource and sets generates a unique Session::id. ~Session(); // Frees the resource Session(const Session&) =…
John O'brien
  • 321
  • 1
  • 9
2
votes
1 answer

Is there a macro to make std::optional::operator* throw on bad access?

I mistakenly have dereferenced an uninitialized std::optional and only found out using valgrind (Syscall param exit_group(status) contains uninitialised byte(s)), like in this minimal working example: #include struct Foo { int…
phinz
  • 1,225
  • 10
  • 21
2
votes
2 answers

C++ compiler says "inconsistent deduction for auto return type"

There is this nice feature in C++ where you can say the function has return type "auto" and compiler will figure it out. However, what if I return a pointer and nullptr on error? Somehow compiler then fails to deduce the correct type and gives an…
user45927
  • 186
  • 6
2
votes
2 answers

C++ Safe Recursive Dereference

I would like an elegant way to safely read data in a field which is wrapped in "nullable types" such as std::optional and std::shared_ptr. Take as example: #include #include #include struct Entry { …
2
votes
1 answer

Returning an std::optional without copying

I'm currently learning OpenGL and while writing a shader abstraction class I chose std::optional for error handling. Now to prevent accidental double freeing, I'm removing the copy constructors with // Shader.h Shader(const Shader&) =…
CANVAS
  • 31
  • 1
  • 4
2
votes
1 answer

Does std::optional copy an object containing a std::unique_ptr

I'm trying to return a std::optional to an object containing a std::unique_ptr member. However, when I try to change the optional return type to use a reference, I get errors like this: error: cannot bind non-const lvalue reference of type…
bcguy
  • 21
  • 3
2
votes
0 answers

MSVC (std:c++17) Optional always returning a value

I'm writing some intersection code for a Ray tracer. After calculating some intersection logic for a sphere, I want to return an optional to determine if there was an intersection or not. Instead of returning an "invalid" value for distance, I want…
2
votes
1 answer

How to set a string to an optional string value?

Due to some constraint in the program(C++), I have a case where I am assigning an optional string to a string variable, which give the following error: error: no match for ‘operator=’ ... The piece of code is something like: void…
DonBaka
  • 325
  • 2
  • 14
2
votes
3 answers

std::optional vs default argument

In a discussion with a colleague regarding the use of std::optional this came up. Say we have a function that takes 2 unique pointers and the 2nd argument is optional i.e. it can be null. It can be implemented either via using std::optional or just…
Hummingbird
  • 647
  • 8
  • 27
2
votes
1 answer

How to pass std::optional struct that contains std::unique_ptr to a function?

I am learning how to use std::optional, and I am having trouble passing a std::optional parameter to a function, since Type contains within it a std::unique_ptr, which prevents the call. What is the correct way to pass such a variable…
2
votes
2 answers

What object is produced by ternary operator in C++?

The following program #include #include int main() { std::optional a; constexpr bool x = true; const std::optional & b = x ? a : std::nullopt; std::cout << (&a == &b); const std::optional &…
Fedor
  • 17,146
  • 13
  • 40
  • 131
2
votes
1 answer

Why is std::optional's assignment operator not usable in compile-time context in this simple example?

After fiddling with the Compiler Explorer (as well as reading cppref.com on std::optional) for half an hour, I give up. Not much else to say other than I don't understand why this code doesn't compile. Someone please explain this, and maybe show me…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
2
votes
3 answers

Is there a less verbose idiom for unpacking an optional in C++?

In the project I am currently working on I find myself writing a lot of code that looks like the following, where, get_optional_foo is returning an std::optional: //... auto maybe_foo = get_optional_foo(quux, ...) if (!maybe_foo.has_value()) …
jwezorek
  • 8,592
  • 1
  • 29
  • 46
2
votes
1 answer

How to rewrite with boost::optional in C++11?

How do I rewrite the following code to use boost::optional or boost::none, in C++11? std::unique_ptr find( std::string key) { std::map(void)> > m{ {"key1",…
javauser
  • 31
  • 1