boost::optional alternative in C++ Standard Library

12,626

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
12,626
Dmitry Bespalov
Author by

Dmitry Bespalov

Make some code every day. That's all.

Updated on June 06, 2022

Comments

  • Dmitry Bespalov
    Dmitry Bespalov almost 2 years

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

  • Dai Doan
    Dai Doan over 12 years
    In some environments, it is politically impossible to use boost, sadly.
  • Nova
    Nova about 11 years
    Have you thought about extracting just optional from boost?
  • Nova
    Nova over 10 years
    Apparently it has just been voted out C++14 into a Technical Specification (see en.cppreference.com/w/cpp/utility/optional) :(
  • the_mandrill
    the_mandrill almost 9 years
    Isn't it the case that 'voting out' in C++ committee-speak actually means 'voting for' ?
  • Kit10
    Kit10 almost 8 years
    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.
  • pmr
    pmr almost 8 years
    @Copperpot There you go. I actually removed that piece of opinion.
  • codeshot
    codeshot almost 8 years
    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)
  • MuchToLearn
    MuchToLearn over 6 years
    @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?
  • codeshot
    codeshot over 6 years
    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 sensibly
  • Ruslan
    Ruslan almost 5 years
    Update: std::optional is part of C++17.
  • Enlico
    Enlico about 4 years
    @pmr, please, update your question to include C++17.