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

- 18,345
- 14
- 76
- 126

- 5,179
- 3
- 26
- 33
-
C++0x == C++11, removed from question. :) – Xeo Jan 15 '12 at 15:10
-
1You 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 http://www.boost.org/doc/libs/1_53_0/libs/optional/doc/html/boost_optional/development.html#boost_optional.development.the_interface "As a result, you might be able to replace optional
by T* on some situations but not always" – alfC May 10 '13 at 08:43
3 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.

- 58,701
- 10
- 113
- 156
-
12In some environments, it is politically impossible to use boost, sadly. – Brian Neal Jan 15 '12 at 17:51
-
3Have you thought about [extracting just optional from boost](http://www.boost.org/doc/libs/1_53_0/tools/bcp/doc/html/index.html)? – Nova Mar 28 '13 at 15:25
-
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. – Kit10 Jun 03 '16 at 21:38
-
-
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. http://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) – codeshot Jun 09 '16 at 21:46
-
@codeshot 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? – MuchToLearn Aug 08 '17 at 22:13
-
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
> - which is an important use case for implementing formally defined programs but those always need to compose sensibly – codeshot Aug 09 '17 at 20:31 -
2Update: [`std::optional`](https://en.cppreference.com/w/cpp/utility/optional) is part of C++17. – Ruslan Jul 12 '19 at 12:56
-
There is currently a proposal for C++14 (or C++17). So the answer is (probably) not yet :).
-
4Apparently it has just been voted out C++14 into a Technical Specification (see http://en.cppreference.com/w/cpp/utility/optional) :( – Nova Oct 02 '13 at 07:56
-
1Isn't it the case that 'voting out' in C++ committee-speak actually means 'voting for' ? – the_mandrill Jul 30 '15 at 15:57
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).