boost::optional alternative in C++ Standard Library
Asked Answered
T

3

22

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)?

Teheran answered 15/1, 2012 at 14:3 Comment(2)
C++0x == C++11, removed from question. :)Alonaalone
You can use a nullable pointer as a cheap and unsafe alternative to boost::optional. Or maybe std::unique_ptr, where the value 0 (null_ptr, NULL) is interpreted an uninitialized optional type. Besides the drawbacks the good thing is that you dont need to implement anything; but just keep the abstraction in mind. A clue about this is given in the boost manual itself boost.org/doc/libs/1_53_0/libs/optional/doc/html/boost_optional/… "As a result, you might be able to replace optional<T> by T* on some situations but not always"Shirlyshiroma
U
26

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.

Uncommunicative answered 15/1, 2012 at 14:38 Comment(9)
In some environments, it is politically impossible to use boost, sadly.Hystero
Have you thought about extracting just optional from boost?Padus
Good answer, except for the ", but why would you try to get rid of boost anyway?" - Like others have posted, Boost isn't always feasible; not to mention, including such a massive library (and all the headaches of managing version dependencies) for one very light-weight feature is not ideal.Sain
@Copperpot There you go. I actually removed that piece of opinion.Uncommunicative
Personally, I'd recommend avoiding boost::optional. This is primarily because operator= assigns the stored object instead of destroying and copy/move constructing. It seems like a cool feature because it looks like it logically means the right thing but that's only true for an lvalue, not an xvalue - which is what is held by boost optional. en.cppreference.com/w/cpp/types/aligned_storage makes it very easy to roll your own that behaves correctly (it's just five or six lines)Cana
@Cana I'm a little confused here, shouldn't the stored object's type have a suitable operator= defined for it that knows what to do in each case?Overwrite
You can't have operator= on a constant object or a reference. So you can't even express such a program. The operator= of the instance of optional itself would need to be specialised for when there can be no correct operator= following sensible laws so that it remakes the stored object entirely. That might be confusing to some but okay for others. unfortunately N3793 section X.Y.4.3 has operator=(nullopt_t) which will be a problem for optional<optional<T>> - which is an important use case for implementing formally defined programs but those always need to compose sensiblyCana
Update: std::optional is part of C++17.Asinine
@pmr, please, update your question to include C++17.Davisdavison
P
13

There is currently a proposal for C++14 (or C++17). So the answer is (probably) not yet :).

Padus answered 9/11, 2012 at 13:0 Comment(2)
Apparently it has just been voted out C++14 into a Technical Specification (see en.cppreference.com/w/cpp/utility/optional) :(Padus
Isn't it the case that 'voting out' in C++ committee-speak actually means 'voting for' ?Flitch
C
1

Like pmr explained, it is not possible right now, and will not be until C++17 is out.

However, you should be able to use this single header library on github as a drop in replacement of boost- or std optional. It has no dependencies (except a c++11/c++14 capable compiler).

Conant answered 30/7, 2015 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.