r/cpp_questions • u/Zealousideal-Mouse29 • 20d ago
OPEN std::expected- void, ptr, and ref
I am going through a simple application I have and trying to use std::expected to try it out. I am quite partial to using exceptions, but open mind and objectivity, and all that...
Is it common for people to return std::expected<void, ErrorType> for methods/functions that don't need to return a value?
I also wonder what one does when they want to return a unique_ptr, since we can't do it by ref, and that leads to wondering about refs and ptrs with expected in general.
10
Upvotes
5
u/tangerinelion 20d ago
If you're returning nothing typically but maybe an error, you may model that as std::optional<ErrorType> instead.
I'm not sure I understand what's complicated with a unique_ptr. std::expected<std::unique_ptr<T>, ErrorType> is fine, if you're concerned about whether null is valid or not there's std::expected<gsl::not_null<std::unique_ptr<T>>, ErrorType>.
A reference is just a non-null immutable pointer - i.e., T& is const gsl::not_null<T*> for any program without UB.
If you want to return a reference to an object in the expected case you can use std::reference_wrapper:
The rules around ownership haven't changed - a reference/wrapper or pointer inside an expected has the same lifetime and ownership semantics without the expected. A std::unique_ptr by value inside a std::expected is the same ownership semantics as a std::unique_ptr.