I'm writing a small C++11 library in which I believe std::optional
would be a nice addition in some functions which can return nullptr
. However, std::optional
is a C++17 feature. Since being C++11 is a requirement, I'm looking for ways to use std::optional
while keeping compatibility.
I found that feature macros can be tested. I suppose I could use it to detect whether std::optional
is available... but what's the best approach when it isn't?
Should I provide my own std::optional
implementation?
Return nullptr
when std::optional
isn't available? (Likely to mess my code.)
Or give up on the idea and keep returning nullptr
only?
boost::optional
? – Yonatanstd::optional
. Choose one. – Jaimiestd::unique_ptr
– Shetritstd::optional
is not that complex. Find some reliable implementation and/or write some template code. You won't get that class from anywhere else in C++11. – Cardstd::optional
just use it. Don’t complicate things with maybe-this-way-maybe-that-way. – Supinestd::unique_ptr
. (Note: my company used a std::optional backport to C++11, so it is possible to use it with C++11.) – Marasmus