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)?
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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With