Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::optional alternative in C++ Standard Library

Tags:

I'm trying to get my program working without boost usage, but can't find an alternative of some useful patterns. Namely, I can't find boost::optional-likewise pattern in the standard library. Is there some standard alternative for boost::optional (C++11 or somewhere else)?

like image 965
Dmitry Bespalov Avatar asked Jan 15 '12 14:01

Dmitry Bespalov


People also ask

What is boost :: optional?

boost::optional<T> provides an alternative way to support the null data condition and as such relieves the user from necessity to handle separate indicator values. The boost::optional<T> objects can be used everywhere where the regular user provided values are expected.

What is std :: optional C++?

The class template std::optional manages an optional contained value, i.e. a value that may or may not be present. A common use case for optional is the return value of a function that may fail.

Does STD optional allocate?

What's more, std::optional doesn't need to allocate any memory on the free store. std::optional is a part of C++ vocabulary types along with std::any , std::variant and std::string_view .

What is boost none?

The expression boost::none denotes an instance of boost::none_t that can be used as the parameter. Example: #include <boost/none.hpp> optional<int> n(boost::none) ; assert ( ! n ) ; optional<T (not a ref)>::optional( T const& v ) Effect: Directly-Constructs an optional.


1 Answers

Short answer: No.

Long answer: Roll your own according to the boost spec. The documentation is quite exhaustive and the code isn't that complex, but this still requires above average C++ skills.

To update this answer: C++14 unfortunately did not ship with std::optional. The current proposal (Revision 5) is N3793 and it is expected to be shipped as a separate technical specification or to become part of C++17.

like image 74
pmr Avatar answered Sep 17 '22 15:09

pmr