Files
variant2/README.md

65 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2017-05-29 16:28:58 +03:00
# variant2
2017-06-01 02:48:27 +03:00
2018-10-18 19:55:15 +03:00
This repository contains a never-valueless C++11/14/17 implementation of
[std::variant](http://en.cppreference.com/w/cpp/utility/variant) in
[variant.hpp](include/boost/variant2/variant.hpp) and an implementation of
`expected<T, E...>` in [expected.hpp](include/boost/variant2/expected.hpp)
that is an extended version of `expected<T, E>` as proposed in
[P0323R1](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0323r1.pdf)
and the subsequent
[D0323R2](https://github.com/viboes/std-make/blob/master/doc/proposal/expected/d0323r2.md).
2017-06-01 02:48:27 +03:00
2018-10-18 19:55:15 +03:00
The code requires [Boost.Mp11](https://github.com/boostorg/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
2019-02-22 22:04:33 +02:00
[standalone](https://godbolt.org/z/CTZztA).
2017-06-01 02:48:27 +03:00
Supported compilers:
2018-10-19 01:24:26 +03:00
* g++ 4.8 or later with `-std=c++11` or above
* clang++ 3.5 or later with `-std=c++11` or above
2018-10-18 19:55:15 +03:00
* Visual Studio 2015, 2017
2017-06-03 14:24:41 +03:00
2018-10-18 19:55:15 +03:00
Tested on [Travis](https://travis-ci.org/pdimov/variant2/) and
[Appveyor](https://ci.appveyor.com/project/pdimov/variant2/).
2017-06-13 22:01:43 +03:00
2017-06-03 14:24:41 +03:00
## variant.hpp
2018-10-18 19:55:15 +03:00
The class `boost::variant2::variant<T...>` is an almost conforming
implementation of `std::variant` with the following differences:
2017-06-03 14:24:41 +03:00
2018-10-18 19:55:15 +03:00
* A converting constructor from, e.g. `variant<int, float>` to
`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.
2017-06-03 14:24:41 +03:00
2018-10-18 19:55:15 +03:00
To avoid going into a valueless-by-exception state, this implementation falls
back to using double storage unless
2017-06-03 14:24:41 +03:00
2018-10-18 19:55:15 +03:00
* one of the alternatives is the type `monostate`,
2018-10-18 20:24:06 +03:00
* one of the alternatives has a nonthrowing default constructor, or
2018-10-18 19:55:15 +03:00
* all the contained types are nothrow move constructible.
2017-06-03 14:24:41 +03:00
2018-10-19 01:24:26 +03:00
If the first two bullets don't hold, but the third does, the variant uses
2018-10-18 19:55:15 +03:00
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
can force `emplace` into always constructing in-place by adding `monostate` as
one of the alternatives.
2017-06-03 14:24:41 +03:00
## expected.hpp
2018-10-18 19:55:15 +03:00
The class `boost::variant2::expected<T, E...>` represents the return type of
an operation that may potentially fail. It contains either the expected result
of type `T`, or a reason for the failure, of one of the error types in `E...`.
Internally, this is stored as `variant<T, E...>`.
2017-06-03 15:58:27 +03:00
See [its documentation](doc/expected.md) for more information.
2018-10-18 19:55:15 +03:00
Note that, while `variant` is production quality, `expected` is still a work
in progress and has no test suite yet.