Best alternative to std::optional to return an optional value from a method? (using C++98/C++11/C++14)
std::optional
, like its boost::optional
parent, is a pretty basic class template. It's a bool
, some storage, and a bunch of convenience member functions most of which are one line of code and an assert.
The DIY option is definitely preferred. (1) involves allocation and (2), (3) involve having to construct a T
even if you want a null value - which doesn't matter at all for double
but does matter for more expensive types. With (5), exceptions are not a replacement for optional
.
You can always compare your implementation to Boost's. It's a small header-only library, after all.