This repository contains a never-valueless C++11/14/17 implementation of
variant.hpp and an implementation of
expected<T, E...> in expected.hpp
that is an extended version of
expected<T, E> as proposed in
and the subsequent
The code requires Boost.Mp11 and Boost.Config.
The repository is intended to be placed into the
libs/variant2 directory of
a Boost clone or release, but the header
variant.hpp will also work
- g++ 4.8 or later with
- clang++ 3.5 or later with
- Visual Studio 2015, 2017
boost::variant2::variant<T...> is an almost conforming
std::variant with the following differences:
- A converting constructor from, e.g.
variant<float, double, int>is provided as an extension;
- The reverse operation, going from
variant<float, double, int>to
variant<int, float>is provided as the member function
subset<U...>. (This operation can throw if the current state of the variant cannot be represented.)
variant<T...>is not trivial when all contained types are trivial.
To avoid going into a valueless-by-exception state, this implementation falls back to using double storage unless
- one of the alternatives is the type
- one of the alternatives has a nonthrowing default constructor, or
- all the contained types are nothrow move constructible.
If the first two bullets don't hold, but the third does, the variant uses
single storage, but
emplace constructs a temporary and moves it into place
if the construction of the object can throw. In case this is undesirable, one
emplace into always constructing in-place by adding
one of the alternatives.
boost::variant2::expected<T, E...> represents the return type of
an operation that may potentially fail. It contains either the expected result
T, or a reason for the failure, of one of the error types in
Internally, this is stored as
See its documentation for more information.
Note that, while
variant is production quality,
expected is still a work
in progress and has no test suite yet.