Merge branch 'develop'

This commit is contained in:
Peter Dimov
2017-06-01 02:51:04 +03:00
33 changed files with 5023 additions and 1 deletions

225
.travis.yml Normal file
View File

@ -0,0 +1,225 @@
# Copyright 2016 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
language: cpp
sudo: false
python: "2.7"
os:
- linux
- osx
branches:
only:
- master
- develop
env:
matrix:
- BOGUS_JOB=true
matrix:
exclude:
- env: BOGUS_JOB=true
include:
- os: linux
compiler: g++-5
env: TOOLSET=gcc COMPILER=g++-5 CXXSTD=c++14
addons:
apt:
packages:
- g++-5
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-5
env: TOOLSET=gcc COMPILER=g++-5 CXXSTD=c++1z
addons:
apt:
packages:
- g++-5
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-6
env: TOOLSET=gcc COMPILER=g++-6 CXXSTD=c++14
addons:
apt:
packages:
- g++-6
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-6
env: TOOLSET=gcc COMPILER=g++-6 CXXSTD=c++1z
addons:
apt:
packages:
- g++-6
sources:
- ubuntu-toolchain-r-test
- os: linux
dist: trusty
compiler: g++-7
env: TOOLSET=gcc COMPILER=g++-7 CXXSTD=c++14
addons:
apt:
packages:
- g++-7
sources:
- ubuntu-toolchain-r-test
- os: linux
dist: trusty
compiler: g++-7
env: TOOLSET=gcc COMPILER=g++-7 CXXSTD=c++1z
addons:
apt:
packages:
- g++-7
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: clang++-3.5
env: TOOLSET=clang COMPILER=clang++-3.5 CXXSTD=c++14
addons:
apt:
packages:
- clang-3.5
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.5
- os: linux
compiler: clang++-3.6
env: TOOLSET=clang COMPILER=clang++-3.6 CXXSTD=c++14
addons:
apt:
packages:
- clang-3.6
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.6
- os: linux
compiler: clang++-3.7
env: TOOLSET=clang COMPILER=clang++-3.7 CXXSTD=c++14
addons:
apt:
packages:
- clang-3.7
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.7
- os: linux
compiler: clang++-3.8
env: TOOLSET=clang COMPILER=clang++-3.8 CXXSTD=c++14
addons:
apt:
packages:
- clang-3.8
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.8
- os: linux
compiler: clang++-3.8
env: TOOLSET=clang COMPILER=clang++-3.8 CXXSTD=c++1z
addons:
apt:
packages:
- clang-3.8
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.8
- os: linux
compiler: clang++-3.9
env: TOOLSET=clang COMPILER=clang++-3.9 CXXSTD=c++14
addons:
apt:
packages:
- clang-3.9
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.9
- os: linux
compiler: clang++-3.9
env: TOOLSET=clang COMPILER=clang++-3.9 CXXSTD=c++1z
addons:
apt:
packages:
- clang-3.9
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.9
- os: linux
dist: trusty
compiler: clang++-4.0
env: TOOLSET=clang COMPILER=clang++-4.0 CXXSTD=c++14
addons:
apt:
packages:
- clang-4.0
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-4.0
- os: linux
dist: trusty
compiler: clang++-4.0
env: TOOLSET=clang COMPILER=clang++-4.0 CXXSTD=c++1z
addons:
apt:
packages:
- clang-4.0
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-4.0
- os: osx
compiler: clang++
env: TOOLSET=clang COMPILER=clang++ CXXSTD=c++14
- os: osx
compiler: clang++
env: TOOLSET=clang COMPILER=clang++ CXXSTD=c++1z
install:
- cd ..
- git clone -b $TRAVIS_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
- git submodule update --init tools/build
- git submodule update --init libs/config
- git submodule update --init tools/boostdep
- git clone -b $TRAVIS_BRANCH https://github.com/pdimov/mp11 libs/mp11
- mkdir libs/variant2
- cp -r $TRAVIS_BUILD_DIR/* libs/variant2
- python tools/boostdep/depinst/depinst.py variant2
- ./bootstrap.sh
- ./b2 headers
script:
- |-
echo "using $TOOLSET : : $COMPILER : <cxxflags>-std=$CXXSTD ;" > ~/user-config.jam
- ./b2 libs/variant2/test toolset=$TOOLSET
notifications:
email:
on_success: always

View File

@ -1,2 +1,14 @@
# variant2
A never-valueless implementation of std::variant
A never-valueless C++14 implementation of [std::variant](http://en.cppreference.com/w/cpp/utility/variant). Requires [mp11](https://github.com/pdimov/mp11) and Boost.Config. Intended to be placed into the `libs/variant2` directory of a Boost clone or release, with mp11 in `libs/mp11`.
To avoid the valueless state, this implementation falls back to using double storage unless
* all the contained types are nothrow move constructible, or
* at least one contained type has a nothrow default constructor.
Supported compilers:
* g++ 5 or later with -std=c++14 or -std=c++1z
* clang++ 3.5 or later with -std=c++14 or -std=c++1z
* Visual Studio 2017

36
appveyor.yml Normal file
View File

@ -0,0 +1,36 @@
# Copyright 2016 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
version: 1.0.{build}-{branch}
shallow_clone: true
branches:
only:
- master
- develop
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
TOOLSET: msvc-14.1
install:
- cd ..
- git clone -b %APPVEYOR_REPO_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
- git submodule update --init tools/build
- git submodule update --init libs/config
- git submodule update --init tools/boostdep
- git clone -b %APPVEYOR_REPO_BRANCH% https://github.com/pdimov/mp11 libs/mp11
- mkdir libs\variant2
- xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\variant2
- python tools/boostdep/depinst/depinst.py variant2
- bootstrap
- b2 headers
build: off
test_script:
- b2 libs/variant2/test toolset=%TOOLSET%

File diff suppressed because it is too large Load Diff

14
meta/libraries.json Normal file
View File

@ -0,0 +1,14 @@
{
"key": "variant2",
"name": "Variant2",
"authors": [
"Peter Dimov"
],
"maintainers": [
"Peter Dimov <pdimov -at- pdimov.com>"
],
"description": "A never-valueless implementation of std::variant.",
"category": [
"Containers", "Data"
]
}

40
test/Jamfile Normal file
View File

@ -0,0 +1,40 @@
# Boost.Variant2 Library Test Jamfile
#
# Copyright 2015-2017 Peter Dimov
#
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt
import testing ;
import ../../config/checks/config : requires ;
REQ = ; #[ requires cxx11_variadic_templates cxx11_template_aliases cxx11_decltype cxx11_hdr_type_traits ] ;
run variant_size.cpp : : : $(REQ) ;
run variant_alternative.cpp : : : $(REQ) ;
run variant_holds_alternative.cpp : : : $(REQ) ;
compile variant_holds_alternative_cx.cpp : : : $(REQ) ;
run variant_get_by_index.cpp : : : $(REQ) ;
compile variant_get_by_index_cx.cpp : : : $(REQ) ;
run variant_get_by_type.cpp : : : $(REQ) ;
compile variant_get_by_type_cx.cpp : : : $(REQ) ;
run variant_default_construct.cpp : : : $(REQ) ;
compile variant_default_construct_cx.cpp : : : $(REQ) ;
run variant_copy_construct.cpp : : : $(REQ) ;
run variant_move_construct.cpp : : : $(REQ) ;
run variant_value_construct.cpp : : : $(REQ) ;
compile variant_value_construct_cx.cpp : : : $(REQ) ;
run variant_in_place_index_construct.cpp : : : $(REQ) ;
compile variant_in_place_index_construct_cx.cpp : : : $(REQ) ;
run variant_in_place_type_construct.cpp : : : $(REQ) ;
compile variant_in_place_type_construct_cx.cpp : : : $(REQ) ;
run variant_copy_assign.cpp : : : $(REQ) ;
run variant_move_assign.cpp : : : $(REQ) ;
run variant_value_assign.cpp : : : $(REQ) ;
run variant_emplace_index.cpp : : : $(REQ) ;
run variant_emplace_type.cpp : : : $(REQ) ;
run variant_swap.cpp : : : $(REQ) ;
run variant_eq_ne.cpp : : : $(REQ) ;
run variant_destroy.cpp : : : $(REQ) ;
run variant_visit.cpp : : : $(REQ) ;

View File

@ -0,0 +1,89 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/mp11.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <cstddef>
using namespace boost::variant2;
using namespace boost::mp11;
template<class I, class T> using var_alt_t = variant_alternative_t<I::value, T>;
int main()
{
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void>>, void>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void> const>, void const>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void> volatile>, void volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void> const volatile>, void const volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int>>, void>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int> const>, void const>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int> volatile>, void volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int> const volatile>, void const volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int>>, int>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int> const>, int const>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int> volatile>, int volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int> const volatile>, int const volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int, float>>, void>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int, float> const>, void const>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int, float> volatile>, void volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<0, variant<void, int, float> const volatile>, void const volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int, float>>, int>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int, float> const>, int const>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int, float> volatile>, int volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<1, variant<void, int, float> const volatile>, int const volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<2, variant<void, int, float>>, float>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<2, variant<void, int, float> const>, float const>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<2, variant<void, int, float> volatile>, float volatile>));
BOOST_TEST_TRAIT_TRUE((std::is_same<variant_alternative_t<2, variant<void, int, float> const volatile>, float const volatile>));
variant_alternative<0, void>();
variant_alternative<0, void const>();
variant_alternative<0, void volatile>();
variant_alternative<0, void const volatile>();
variant_alternative<0, variant<>>();
variant_alternative<0, variant<> const>();
variant_alternative<0, variant<> volatile>();
variant_alternative<0, variant<> const volatile>();
variant_alternative<1, variant<int>>();
variant_alternative<1, variant<int> const>();
variant_alternative<1, variant<int> volatile>();
variant_alternative<1, variant<int> const volatile>();
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, void const>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, void volatile>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, void const volatile>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, variant<>>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, variant<> const>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, variant<> volatile>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<0>, variant<> const volatile>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_alt_t, mp_size_t<0>, variant<int>>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_alt_t, mp_size_t<0>, variant<int> const>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_alt_t, mp_size_t<0>, variant<int> volatile>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_alt_t, mp_size_t<0>, variant<int> const volatile>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<1>, variant<int>>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<1>, variant<int> const>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<1>, variant<int> volatile>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_alt_t, mp_size_t<1>, variant<int> const volatile>));
return boost::report_errors();
}

View File

@ -0,0 +1,194 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
struct Y
{
Y& operator=( Y const& ) = delete;
};
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int> v2( 1 );
v = v2;
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int> const v3( 2 );
v = v3;
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, float> v2( 1 );
v = v2;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int, float> v3( 3.14f );
v = v3;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
variant<int, float> const v4( 3.15f );
v = v4;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, int, float, std::string> v2( in_place_index<1>, 1 );
v = v2;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
variant<int, int, float, std::string> v3( 3.14f );
v = v3;
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
variant<int, int, float, std::string> const v4( 3.15f );
v = v4;
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
variant<int, int, float, std::string> v5( "s1" );
v = v5;
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
variant<int, int, float, std::string> const v6( "s2" );
v = v6;
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
variant<X1, X2> v2( X1{1} );
v = v2;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
variant<X1, X2> v3( in_place_index<1>, 2 );
v = v3;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
variant<X1, X2> const v4( in_place_index<1>, 3 );
v = v4;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 3 );
variant<X1, X2> const v5( in_place_index<0>, 4 );
v = v5;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
{
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable<variant<int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable<variant<int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable<variant<int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_assignable<variant<int, int, float, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<X1, int>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<X1, int, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<int, int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_assignable<variant<X1, X2, int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_copy_assignable<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_copy_assignable<variant<int const>>));
BOOST_TEST_TRAIT_FALSE((std::is_copy_assignable<variant<int, float, Y>>));
}
return boost::report_errors();
}

View File

@ -0,0 +1,128 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
X1() {}
X1(X1 const&) {}
X1(X1&&) {}
};
inline bool operator==( X1, X1 ) { return true; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
struct X2
{
X2() {}
X2(X2 const&) {}
X2(X2&&) {}
};
inline bool operator==( X2, X2 ) { return true; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
struct Y
{
Y( Y const& ) = delete;
};
template<class V> static void test( V const & v )
{
V v2( v );
BOOST_TEST_EQ( v.index(), v2.index() );
BOOST_TEST( v == v2 );
}
int main()
{
test( variant<int>() );
test( variant<int>(1) );
test( variant<int const>() );
test( variant<int const>(1) );
test( variant<int, float>() );
test( variant<int, float>(1) );
test( variant<int, float>(3.14f) );
test( variant<int, float, std::string>() );
test( variant<int, float, std::string>(1) );
test( variant<int, float, std::string>(3.14f) );
test( variant<int, float, std::string>("test") );
test( variant<int, int>() );
test( variant<int, int, float>() );
test( variant<int, int, float>(3.14f) );
test( variant<int, int, float, float>() );
test( variant<int, int, float, float, std::string>("test") );
test( variant<std::string, std::string, float>() );
test( variant<X1, X2>() );
test( variant<X1, X2, int>() );
test( variant<X1, X2, X2>() );
test( variant<X1, X1, X2, X2>() );
{
variant<X1, X2> v;
v.emplace<X2>();
test( v );
}
{
variant<X1, X1, X2> v;
v.emplace<X2>();
test( v );
}
{
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible<variant<int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible<variant<int const>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible<variant<int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible<variant<int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible<variant<int, int, float, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<X1, int>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<X1, int, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<int, int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible<variant<X1, X2, int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_copy_constructible<variant<int, float, Y>>));
}
return boost::report_errors();
}

View File

@ -0,0 +1,103 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
X();
};
struct Y
{
Y() = delete;
};
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int const> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, int, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), std::string() );
}
{
variant<std::string const> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), std::string() );
}
{
variant<std::string, int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), std::string() );
}
{
variant<std::string, std::string, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), std::string() );
}
{
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible<variant<int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible<variant<int const>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible<variant<int, X>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible<variant<int, float, X>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible<variant<int, int, X>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_default_constructible<variant<int, X, X>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_default_constructible<variant<X>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_default_constructible<variant<X, int>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_default_constructible<variant<X, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_default_constructible<variant<int, Y>>));
BOOST_TEST_TRAIT_FALSE((std::is_default_constructible<variant<Y, int>>));
}
return boost::report_errors();
}

View File

@ -0,0 +1,51 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v;
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
{
constexpr variant<int const> v;
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
{
constexpr variant<int, float> v;
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
{
constexpr variant<int, int, float> v;
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
{
constexpr variant<int, float, float> v;
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
}

205
test/variant_destroy.cpp Normal file
View File

@ -0,0 +1,205 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X1
{
static int instances;
int v;
X1(): v(0) { ++instances; }
explicit X1(int v): v(v) { ++instances; }
X1(X1 const& r): v(r.v) { ++instances; }
X1(X1&& r): v(r.v) { ++instances; }
~X1() noexcept { --instances; }
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
int X1::instances = 0;
struct X2
{
static int instances;
int v;
X2(): v(0) { ++instances; }
explicit X2(int v): v(v) { ++instances; }
X2(X2 const& r): v(r.v) { ++instances; }
X2(X2&& r): v(r.v) { ++instances; }
~X2() noexcept { --instances; }
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
int X2::instances = 0;
struct Y1
{
static int instances;
int v;
Y1() noexcept: v(0) { ++instances; }
explicit Y1(int v) noexcept: v(v) { ++instances; }
Y1(Y1 const& r) noexcept: v(r.v) { ++instances; }
Y1(Y1&& r) noexcept: v(r.v) { ++instances; }
~Y1() noexcept { --instances; }
Y1& operator=( Y1 const& r ) noexcept { v = r.v; return *this; }
Y1& operator=( Y1&& r ) noexcept { v = r.v; return *this; }
};
int Y1::instances = 0;
struct Y2
{
static int instances;
int v;
Y2() noexcept: v(0) { ++instances; }
explicit Y2(int v) noexcept: v(v) { ++instances; }
Y2(Y2 const& r) noexcept: v(r.v) { ++instances; }
Y2(Y2&& r) noexcept: v(r.v) { ++instances; }
~Y2() noexcept { --instances; }
Y2& operator=( Y2 const& r ) noexcept { v = r.v; return *this; }
Y2& operator=( Y2&& r ) noexcept { v = r.v; return *this; }
};
int Y2::instances = 0;
int main()
{
BOOST_TEST_EQ( Y1::instances, 0 );
{
variant<Y1> v;
BOOST_TEST_EQ( Y1::instances, 1 );
{
variant<Y1> v2;
BOOST_TEST_EQ( Y1::instances, 2 );
v = v2;
BOOST_TEST_EQ( Y1::instances, 2 );
}
BOOST_TEST_EQ( Y1::instances, 1 );
v = Y1{1};
BOOST_TEST_EQ( Y1::instances, 1 );
}
BOOST_TEST_EQ( Y1::instances, 0 );
BOOST_TEST_EQ( Y2::instances, 0 );
{
variant<Y1, Y2> v;
BOOST_TEST_EQ( Y1::instances, 1 );
BOOST_TEST_EQ( Y2::instances, 0 );
{
variant<Y1, Y2> v2;
BOOST_TEST_EQ( Y1::instances, 2 );
BOOST_TEST_EQ( Y2::instances, 0 );
v = v2;
BOOST_TEST_EQ( Y1::instances, 2 );
BOOST_TEST_EQ( Y2::instances, 0 );
v2 = Y2{1};
BOOST_TEST_EQ( Y1::instances, 1 );
BOOST_TEST_EQ( Y2::instances, 1 );
v = v2;
BOOST_TEST_EQ( Y1::instances, 0 );
BOOST_TEST_EQ( Y2::instances, 2 );
}
BOOST_TEST_EQ( Y1::instances, 0 );
BOOST_TEST_EQ( Y2::instances, 1 );
v.emplace<0>();
BOOST_TEST_EQ( Y1::instances, 1 );
BOOST_TEST_EQ( Y2::instances, 0 );
v.emplace<Y2>();
BOOST_TEST_EQ( Y1::instances, 0 );
BOOST_TEST_EQ( Y2::instances, 1 );
}
BOOST_TEST_EQ( Y1::instances, 0 );
BOOST_TEST_EQ( Y2::instances, 0 );
BOOST_TEST_EQ( X1::instances, 0 );
BOOST_TEST_EQ( X2::instances, 0 );
{
variant<X1, X2> v;
BOOST_TEST_EQ( X1::instances, 1 );
BOOST_TEST_EQ( X2::instances, 0 );
{
variant<X1, X2> v2;
BOOST_TEST_EQ( X1::instances, 2 );
BOOST_TEST_EQ( X2::instances, 0 );
v = v2;
BOOST_TEST_EQ( X1::instances, 2 );
BOOST_TEST_EQ( X2::instances, 0 );
v2 = X2{1};
BOOST_TEST_EQ( X1::instances, 1 );
BOOST_TEST_EQ( X2::instances, 1 );
v = v2;
BOOST_TEST_EQ( X1::instances, 0 );
BOOST_TEST_EQ( X2::instances, 2 );
}
BOOST_TEST_EQ( X1::instances, 0 );
BOOST_TEST_EQ( X2::instances, 1 );
v.emplace<0>();
BOOST_TEST_EQ( X1::instances, 1 );
BOOST_TEST_EQ( X2::instances, 0 );
v.emplace<X2>();
BOOST_TEST_EQ( X1::instances, 0 );
BOOST_TEST_EQ( X2::instances, 1 );
}
BOOST_TEST_EQ( X1::instances, 0 );
BOOST_TEST_EQ( X2::instances, 0 );
return boost::report_errors();
}

View File

@ -0,0 +1,181 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<0>();
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<1>( 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
v.emplace<1>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
v.emplace<0>();
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<1>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
v.emplace<1>( 1 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
v.emplace<2>( 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
v.emplace<2>();
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 0 );
v.emplace<3>( "s1" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
v.emplace<3>( "s2" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
v.emplace<3>();
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string() );
v.emplace<3>( { 'a', 'b' } );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), (std::string{ 'a', 'b'}) );
v.emplace<3>( { 'c', 'd' }, std::allocator<char>() );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), (std::string{ 'c', 'd'}) );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
v.emplace<1>( 2 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
v.emplace<0>();
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v.emplace<0>( 4 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
v.emplace<1>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 0 );
v.emplace<1>( 6 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 6 );
v.emplace<0>( 3 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 3 );
v.emplace<0>( 4 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
return boost::report_errors();
}

View File

@ -0,0 +1,169 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<int>( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<int>();
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<int>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<float>( 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
v.emplace<float>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
v.emplace<int>();
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<float>( 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
v.emplace<float>();
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 0 );
v.emplace<std::string>( "s1" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
v.emplace<std::string>( "s2" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
v.emplace<std::string>();
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string() );
v.emplace<std::string>( { 'a', 'b' } );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), (std::string{ 'a', 'b' }) );
v.emplace<std::string>( { 'c', 'd' }, std::allocator<char>() );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), (std::string{ 'c', 'd' }) );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v.emplace<X1>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
v.emplace<X2>( 2 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
v.emplace<X1>();
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v.emplace<X1>( 4 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
v.emplace<X2>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 0 );
v.emplace<X2>( 6 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 6 );
v.emplace<X1>( 3 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 3 );
v.emplace<X1>( 4 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
return boost::report_errors();
}

92
test/variant_eq_ne.cpp Normal file
View File

@ -0,0 +1,92 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
};
inline bool operator==( X const&, X const& ) { return false; }
inline bool operator!=( X const&, X const& ) { return false; }
int main()
{
{
variant<int> v1, v2, v3( 1 ), v4( 1 );
BOOST_TEST( v1 == v2 );
BOOST_TEST_NOT( v1 != v2 );
BOOST_TEST( v1 != v3 );
BOOST_TEST_NOT( v1 == v3 );
BOOST_TEST( v3 == v4 );
BOOST_TEST_NOT( v3 != v4 );
}
{
variant<int, float> v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f );
BOOST_TEST( v1 == v2 );
BOOST_TEST_NOT( v1 != v2 );
BOOST_TEST( v1 != v3 );
BOOST_TEST_NOT( v1 == v3 );
BOOST_TEST( v3 == v4 );
BOOST_TEST_NOT( v3 != v4 );
BOOST_TEST( v1 != v5 );
BOOST_TEST_NOT( v1 == v5 );
BOOST_TEST( v3 != v5 );
BOOST_TEST_NOT( v3 == v5 );
BOOST_TEST( v5 == v6 );
BOOST_TEST_NOT( v5 != v6 );
}
{
variant<int, int, float> v1, v2, v3( in_place_index<1> ), v4( in_place_index<1> ), v5( 3.14f ), v6( 3.14f );
BOOST_TEST( v1 == v2 );
BOOST_TEST_NOT( v1 != v2 );
BOOST_TEST( v1 != v3 );
BOOST_TEST_NOT( v1 == v3 );
BOOST_TEST( v3 == v4 );
BOOST_TEST_NOT( v3 != v4 );
BOOST_TEST( v1 != v5 );
BOOST_TEST_NOT( v1 == v5 );
BOOST_TEST( v3 != v5 );
BOOST_TEST_NOT( v3 == v5 );
BOOST_TEST( v5 == v6 );
BOOST_TEST_NOT( v5 != v6 );
}
{
variant<X> v1, v2;
BOOST_TEST_NOT( v1 == v2 );
BOOST_TEST_NOT( v1 != v2 );
}
return boost::report_errors();
}

View File

@ -0,0 +1,200 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
int main()
{
{
variant<int> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int*>));
}
{
variant<int> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
}
{
variant<int const> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
}
{
variant<int volatile> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const volatile*>));
}
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_EQ( get<0>(std::move(v)), 0 );
}
{
variant<int> v( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_EQ( get<0>(std::move(v)), 1 );
}
{
variant<int, float> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float*>));
}
{
variant<int, float> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float const*>));
}
{
variant<int const, float volatile> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float volatile*>));
}
{
variant<int const, float volatile> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float const volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float const volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float const volatile*>));
}
{
variant<int, float> v;
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 0 );
}
{
variant<int, float> v( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 1 );
}
{
variant<int, float> v( 3.14f );
BOOST_TEST_THROWS( get<0>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<0>(&v), nullptr );
BOOST_TEST_EQ( get<1>(v), 3.14f );
BOOST_TEST_EQ( get_if<1>(&v), &get<1>(v) );
BOOST_TEST_EQ( get<1>(std::move(v)), 3.14f );
}
{
variant<int, float, float> v;
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_THROWS( get<2>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<2>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 0 );
}
{
variant<int, float, float> v( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_THROWS( get<2>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<2>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 1 );
}
{
variant<int, int, float> v( 3.14f );
BOOST_TEST_THROWS( get<0>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<0>(&v), nullptr );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_EQ( get<2>(v), 3.14f );
BOOST_TEST_EQ( get_if<2>(&v), &get<2>(v) );
BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f );
}
return boost::report_errors();
}

View File

@ -0,0 +1,90 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v;
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
}
{
constexpr variant<int> v( 1 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
}
{
constexpr variant<int, float> v;
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
STATIC_ASSERT( get_if<1>(&v) == nullptr );
}
{
constexpr variant<int, float> v( 1 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
STATIC_ASSERT( get_if<1>(&v) == nullptr );
}
{
constexpr variant<int, float> v( 3.14f );
STATIC_ASSERT( get_if<0>(&v) == nullptr );
STATIC_ASSERT( get<1>(v) == 3.14f );
STATIC_ASSERT( get_if<1>(&v) == &get<1>(v) );
}
{
constexpr variant<int, float, float> v;
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
STATIC_ASSERT( get_if<1>(&v) == nullptr );
STATIC_ASSERT( get_if<2>(&v) == nullptr );
}
{
constexpr variant<int, float, float> v( 1 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
STATIC_ASSERT( get_if<1>(&v) == nullptr );
STATIC_ASSERT( get_if<2>(&v) == nullptr );
}
{
constexpr variant<int, int, float> v( 3.14f );
STATIC_ASSERT( get_if<0>(&v) == nullptr );
STATIC_ASSERT( get_if<1>(&v) == nullptr );
STATIC_ASSERT( get<2>(v) == 3.14f );
STATIC_ASSERT( get_if<2>(&v) == &get<2>(v) );
}
}

View File

@ -0,0 +1,166 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
int main()
{
{
variant<int> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
}
{
variant<int> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
}
{
variant<int const> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
}
{
variant<int volatile> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(v)), int const volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(std::move(v))), int const volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int volatile>(&v)), int const volatile*>));
}
{
variant<int> v;
BOOST_TEST_EQ( get<int>(v), 0 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
}
{
variant<int> v( 1 );
BOOST_TEST_EQ( get<int>(v), 1 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
}
{
variant<int, float> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float*>));
}
{
variant<int, float> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float const*>));
}
{
variant<int const, float volatile> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float volatile*>));
}
{
variant<int const, float volatile> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float const volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float const volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float const volatile*>));
}
{
variant<int, float> v;
BOOST_TEST_EQ( get<int>(v), 0 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<float>(&v), nullptr );
}
{
variant<int, float> v( 1 );
BOOST_TEST_EQ( get<int>(v), 1 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<float>(&v), nullptr );
}
{
variant<int, float> v( 3.14f );
BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<int>(&v), nullptr );
BOOST_TEST_EQ( get<float>(v), 3.14f );
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
}
{
variant<int, float, float> v;
BOOST_TEST_EQ( get<int>(v), 0 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
}
{
variant<int, float, float> v( 1 );
BOOST_TEST_EQ( get<int>(v), 1 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
}
{
variant<int, int, float> v( 3.14f );
BOOST_TEST_EQ( get<float>(v), 3.14f );
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
}
return boost::report_errors();
}

View File

@ -0,0 +1,78 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v;
STATIC_ASSERT( get<int>(v) == 0 );
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
}
{
constexpr variant<int> v( 1 );
STATIC_ASSERT( get<int>(v) == 1 );
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
}
{
constexpr variant<int, float> v;
STATIC_ASSERT( get<int>(v) == 0 );
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
STATIC_ASSERT( get_if<float>(&v) == nullptr );
}
{
constexpr variant<int, float> v( 1 );
STATIC_ASSERT( get<int>(v) == 1 );
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
STATIC_ASSERT( get_if<float>(&v) == nullptr );
}
{
constexpr variant<int, float> v( 3.14f );
STATIC_ASSERT( get_if<int>(&v) == nullptr );
STATIC_ASSERT( get<float>(v) == 3.14f );
STATIC_ASSERT( get_if<float>(&v) == &get<float>(v) );
}
{
constexpr variant<int, float, float> v;
STATIC_ASSERT( get<int>(v) == 0 );
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
}
{
constexpr variant<int, float, float> v( 1 );
STATIC_ASSERT( get<int>(v) == 1 );
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
}
{
constexpr variant<int, int, float> v( 3.14f );
STATIC_ASSERT( get<float>(v) == 3.14f );
STATIC_ASSERT( get_if<float>(&v) == &get<float>(v) );
}
}

View File

@ -0,0 +1,78 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <string>
using namespace boost::variant2;
int main()
{
{
variant<int> v;
BOOST_TEST( holds_alternative<int>( v ) );
}
{
variant<int, float> v;
BOOST_TEST( holds_alternative<int>( v ) );
BOOST_TEST( !holds_alternative<float>( v ) );
}
{
variant<int, float> v( 3.14f );
BOOST_TEST( !holds_alternative<int>( v ) );
BOOST_TEST( holds_alternative<float>( v ) );
}
{
variant<int, float, float> v;
BOOST_TEST( holds_alternative<int>( v ) );
}
{
variant<int, int, float> v( 3.14f );
BOOST_TEST( holds_alternative<float>( v ) );
}
{
variant<int, float, std::string> v;
BOOST_TEST( holds_alternative<int>( v ) );
BOOST_TEST( !holds_alternative<float>( v ) );
BOOST_TEST( !holds_alternative<std::string>( v ) );
}
{
variant<int, float, std::string> v( 3.14f );
BOOST_TEST( !holds_alternative<int>( v ) );
BOOST_TEST( holds_alternative<float>( v ) );
BOOST_TEST( !holds_alternative<std::string>( v ) );
}
{
variant<int, float, std::string> v( "text" );
BOOST_TEST( !holds_alternative<int>( v ) );
BOOST_TEST( !holds_alternative<float>( v ) );
BOOST_TEST( holds_alternative<std::string>( v ) );
}
{
variant<int, int, float, std::string> v( 3.14f );
BOOST_TEST( holds_alternative<float>( v ) );
BOOST_TEST( !holds_alternative<std::string>( v ) );
}
{
variant<int, int, float, std::string> v( "text" );
BOOST_TEST( !holds_alternative<float>( v ) );
BOOST_TEST( holds_alternative<std::string>( v ) );
}
return boost::report_errors();
}

View File

@ -0,0 +1,43 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v;
STATIC_ASSERT( holds_alternative<int>( v ) );
}
{
constexpr variant<int, float> v;
STATIC_ASSERT( holds_alternative<int>( v ) );
STATIC_ASSERT( !holds_alternative<float>( v ) );
}
{
constexpr variant<int, float> v( 3.14f );
STATIC_ASSERT( !holds_alternative<int>( v ) );
STATIC_ASSERT( holds_alternative<float>( v ) );
}
{
constexpr variant<int, float, float> v;
STATIC_ASSERT( holds_alternative<int>( v ) );
}
{
constexpr variant<int, int, float> v( 3.14f );
STATIC_ASSERT( holds_alternative<float>( v ) );
}
}

View File

@ -0,0 +1,138 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
X() = default;
X( in_place_index_t<0> ) = delete;
};
int main()
{
{
variant<int> v( in_place_index<0> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<X> v( in_place_index<0> );
BOOST_TEST_EQ( v.index(), 0 );
}
{
variant<int> v( in_place_index<0>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
}
{
variant<int, float> v( in_place_index<0> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, float> v( in_place_index<0>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
}
{
variant<int, float> v( in_place_index<1> );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
}
{
variant<int, float> v( in_place_index<1>, 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<0>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<1>, 1 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<2>, 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<3>, 3.14f );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), 3.14f );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<4>, "text" );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), std::string("text") );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<5>, "text" );
BOOST_TEST_EQ( v.index(), 5 );
BOOST_TEST_EQ( get<5>(v), std::string("text") );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<5>, 4, 'a' );
BOOST_TEST_EQ( v.index(), 5 );
BOOST_TEST_EQ( get<5>(v), std::string( 4, 'a' ) );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<4>, { 'a', 'b', 'c' } );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), (std::string{ 'a', 'b', 'c' }) );
}
{
variant<int, int, float, float, std::string, std::string> v( in_place_index<5>, { 'a', 'b', 'c' }, std::allocator<char>() );
BOOST_TEST_EQ( v.index(), 5 );
BOOST_TEST_EQ( get<5>(v), (std::string{ 'a', 'b', 'c' }) );
}
return boost::report_errors();
}

View File

@ -0,0 +1,116 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
constexpr X() = default;
constexpr explicit X(int, int) {}
X( in_place_index_t<0> ) = delete;
};
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v( in_place_index<0> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
{
constexpr variant<X> v( in_place_index<0> );
STATIC_ASSERT( v.index() == 0 );
}
{
constexpr variant<int> v( in_place_index<0>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
}
{
constexpr variant<int, float> v( in_place_index<0> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
}
{
constexpr variant<int, float> v( in_place_index<0>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
}
{
constexpr variant<int, float> v( in_place_index<1> );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 0 );
}
{
constexpr variant<int, float> v( in_place_index<1>, 3.14f );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 3.14f );
}
{
constexpr variant<int, int, float, float, X, X> v( in_place_index<0>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
}
{
constexpr variant<int, int, float, float, X, X> v( in_place_index<1>, 1 );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 1 );
}
{
constexpr variant<int, int, float, float, X, X> v( in_place_index<2>, 3.14f );
STATIC_ASSERT( v.index() == 2 );
STATIC_ASSERT( get<2>(v) == 3.14f );
}
{
constexpr variant<int, int, float, float, X, X> v( in_place_index<3>, 3.14f );
STATIC_ASSERT( v.index() == 3 );
STATIC_ASSERT( get<3>(v) == 3.14f );
}
{
constexpr variant<int, int, float, float, X, X> v( in_place_index<4> );
STATIC_ASSERT( v.index() == 4 );
}
{
constexpr variant<int, int, float, float, X, X> v( in_place_index<5>, 0, 0 );
STATIC_ASSERT( v.index() == 5 );
}
}

View File

@ -0,0 +1,134 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
X() = default;
template<class T> X( in_place_type_t<T> ) = delete;
};
int main()
{
{
variant<int> v( in_place_type<int> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<X> v( in_place_type<X> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST( holds_alternative<X>(v) );
}
{
variant<int> v( in_place_type<int>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<int, float> v( in_place_type<int> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<int, float> v( in_place_type<int>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<int, float> v( in_place_type<float> );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
BOOST_TEST( holds_alternative<float>(v) );
}
{
variant<int, float> v( in_place_type<float>, 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
BOOST_TEST( holds_alternative<float>(v) );
}
{
variant<int, int, float, std::string> v( in_place_type<float>, 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
BOOST_TEST( holds_alternative<float>(v) );
}
{
variant<int, int, float, float, std::string> v( in_place_type<std::string>, "text" );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), std::string("text") );
BOOST_TEST( holds_alternative<std::string>(v) );
}
{
variant<int, int, float, float, std::string> v( in_place_type<std::string>, 4, 'a' );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), std::string( 4, 'a' ) );
BOOST_TEST( holds_alternative<std::string>(v) );
}
{
variant<int, int, float, float, std::string> v( in_place_type<std::string>, { 'a', 'b', 'c' } );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), (std::string{ 'a', 'b', 'c' }) );
BOOST_TEST( holds_alternative<std::string>(v) );
}
{
variant<int, int, float, float, std::string> v( in_place_type<std::string>, { 'a', 'b', 'c' }, std::allocator<char>() );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), (std::string{ 'a', 'b', 'c' }) );
BOOST_TEST( holds_alternative<std::string>(v) );
}
return boost::report_errors();
}

View File

@ -0,0 +1,110 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
struct X
{
constexpr X() = default;
constexpr explicit X(int, int) {}
template<class T> X( in_place_type_t<T> ) = delete;
};
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v( in_place_type<int> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<X> v( in_place_type<X> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
{
constexpr variant<int> v( in_place_type<int>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<int, float> v( in_place_type<int> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<int, float> v( in_place_type<int>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<int, float> v( in_place_type<float> );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 0 );
STATIC_ASSERT( holds_alternative<float>(v) );
}
{
constexpr variant<int, float> v( in_place_type<float>, 3.14f );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 3.14f );
STATIC_ASSERT( holds_alternative<float>(v) );
}
{
constexpr variant<int, int, float, X> v( in_place_type<float>, 3.14f );
STATIC_ASSERT( v.index() == 2 );
STATIC_ASSERT( get<2>(v) == 3.14f );
STATIC_ASSERT( holds_alternative<float>(v) );
}
{
constexpr variant<int, int, float, float, X> v( in_place_type<X> );
STATIC_ASSERT( v.index() == 4 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
{
constexpr variant<int, int, float, float, X> v( in_place_type<X>, 0, 0 );
STATIC_ASSERT( v.index() == 4 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
}

View File

@ -0,0 +1,194 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
struct Y
{
Y& operator=( Y&& ) = delete;
};
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int> v2( 1 );
v = std::move(v2);
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int> v3( 2 );
v = std::move(v3);
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, float> v2( 1 );
v = std::move(v2);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int, float> v3( 3.14f );
v = std::move(v3);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
variant<int, float> v4( 3.15f );
v = std::move(v4);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, int, float, std::string> v2( in_place_index<1>, 1 );
v = std::move(v2);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
variant<int, int, float, std::string> v3( 3.14f );
v = std::move(v3);
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
variant<int, int, float, std::string> v4( 3.15f );
v = std::move(v4);
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
variant<int, int, float, std::string> v5( "s1" );
v = std::move(v5);
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
variant<int, int, float, std::string> v6( "s2" );
v = std::move(v6);
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
variant<X1, X2> v2( X1{1} );
v = std::move(v2);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
variant<X1, X2> v3( in_place_index<1>, 2 );
v = std::move(v3);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
variant<X1, X2> v4( in_place_index<1>, 3 );
v = std::move(v4);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 3 );
variant<X1, X2> v5( in_place_index<0>, 4 );
v = std::move(v5);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
{
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, int, float, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, int>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, int, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<int, int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, X2, int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_move_assignable<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_move_assignable<variant<int const>>));
BOOST_TEST_TRAIT_FALSE((std::is_move_assignable<variant<int, float, Y>>));
}
return boost::report_errors();
}

View File

@ -0,0 +1,129 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
X1() {}
X1(X1 const&) {}
X1(X1&&) {}
};
inline bool operator==( X1, X1 ) { return true; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
struct X2
{
X2() {}
X2(X2 const&) {}
X2(X2&&) {}
};
inline bool operator==( X2, X2 ) { return true; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
struct Y
{
Y( Y&& ) = delete;
};
template<class V> static void test( V&& v )
{
V v2( v );
V v3( std::move(v) );
BOOST_TEST_EQ( v2.index(), v3.index() );
BOOST_TEST( v2 == v3 );
}
int main()
{
test( variant<int>() );
test( variant<int>(1) );
test( variant<int const>() );
test( variant<int const>(1) );
test( variant<int, float>() );
test( variant<int, float>(1) );
test( variant<int, float>(3.14f) );
test( variant<int, float, std::string>() );
test( variant<int, float, std::string>(1) );
test( variant<int, float, std::string>(3.14f) );
test( variant<int, float, std::string>("test") );
test( variant<int, int>() );
test( variant<int, int, float>() );
test( variant<int, int, float>(3.14f) );
test( variant<int, int, float, float>() );
test( variant<int, int, float, float, std::string>("test") );
test( variant<std::string, std::string, float>() );
test( variant<X1, X2>() );
test( variant<X1, X2, int>() );
test( variant<X1, X2, X2>() );
test( variant<X1, X1, X2, X2>() );
{
variant<X1, X2> v;
v.emplace<X2>();
test( std::move(v) );
}
{
variant<X1, X1, X2> v;
v.emplace<X2>();
test( std::move(v) );
}
{
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int const>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int, int, float, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, int>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, int, float>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<int, int, X1>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, X2, int, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_move_constructible<variant<X1, X2>>));
BOOST_TEST_TRAIT_FALSE((std::is_move_constructible<variant<int, float, Y>>));
}
return boost::report_errors();
}

64
test/variant_size.cpp Normal file
View File

@ -0,0 +1,64 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/mp11.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <cstddef>
using namespace boost::variant2;
using namespace boost::mp11;
template<class T> using var_size_t = mp_size_t<variant_size<T>::value>;
int main()
{
BOOST_TEST_EQ( (variant_size<variant<>>::value), 0 );
BOOST_TEST_EQ( (variant_size<variant<> const>::value), 0 );
BOOST_TEST_EQ( (variant_size<variant<> volatile>::value), 0 );
BOOST_TEST_EQ( (variant_size<variant<> const volatile>::value), 0 );
BOOST_TEST_EQ( (variant_size<variant<void>>::value), 1 );
BOOST_TEST_EQ( (variant_size<variant<void> const>::value), 1 );
BOOST_TEST_EQ( (variant_size<variant<void> volatile>::value), 1 );
BOOST_TEST_EQ( (variant_size<variant<void> const volatile>::value), 1 );
BOOST_TEST_EQ( (variant_size<variant<void, void>>::value), 2 );
BOOST_TEST_EQ( (variant_size<variant<void, void> const>::value), 2 );
BOOST_TEST_EQ( (variant_size<variant<void, void> volatile>::value), 2 );
BOOST_TEST_EQ( (variant_size<variant<void, void> const volatile>::value), 2 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void>>::value), 3 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void> const>::value), 3 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void> volatile>::value), 3 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void> const volatile>::value), 3 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void, void>>::value), 4 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void, void> const>::value), 4 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void, void> volatile>::value), 4 );
BOOST_TEST_EQ( (variant_size<variant<void, void, void, void> const volatile>::value), 4 );
variant_size<void>();
variant_size<void const>();
variant_size<void volatile>();
variant_size<void const volatile>();
BOOST_TEST_TRAIT_FALSE((mp_valid<var_size_t, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_size_t, void const>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_size_t, void volatile>));
BOOST_TEST_TRAIT_FALSE((mp_valid<var_size_t, void const volatile>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_size_t, variant<>>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_size_t, variant<> const>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_size_t, variant<> volatile>));
BOOST_TEST_TRAIT_TRUE((mp_valid<var_size_t, variant<> const volatile>));
return boost::report_errors();
}

259
test/variant_swap.cpp Normal file
View File

@ -0,0 +1,259 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int> v2( 1 );
BOOST_TEST_EQ( get<0>(v2), 1 );
swap( v, v2 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get<0>(v2), 0 );
variant<int> v3( 2 );
BOOST_TEST_EQ( get<0>(v3), 2 );
swap( v, v3 );
BOOST_TEST_EQ( get<0>(v), 2 );
BOOST_TEST_EQ( get<0>(v3), 1 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, float> v2( 1 );
BOOST_TEST_EQ( v2.index(), 0 );
BOOST_TEST_EQ( get<0>(v2), 1 );
swap( v, v2 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( v2.index(), 0 );
BOOST_TEST_EQ( get<0>(v2), 0 );
variant<int, float> v3( 3.14f );
BOOST_TEST_EQ( v3.index(), 1 );
BOOST_TEST_EQ( get<1>(v3), 3.14f );
swap( v, v3 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
BOOST_TEST_EQ( v3.index(), 0 );
BOOST_TEST_EQ( get<0>(v3), 1 );
variant<int, float> v4( 3.15f );
BOOST_TEST_EQ( v4.index(), 1 );
BOOST_TEST_EQ( get<1>(v4), 3.15f );
swap( v, v4 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
BOOST_TEST_EQ( v4.index(), 1 );
BOOST_TEST_EQ( get<1>(v4), 3.14f );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, int, float, std::string> v2( in_place_index<1>, 1 );
BOOST_TEST_EQ( v2.index(), 1 );
BOOST_TEST_EQ( get<1>(v2), 1 );
swap( v, v2 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
BOOST_TEST_EQ( v2.index(), 0 );
BOOST_TEST_EQ( get<0>(v2), 0 );
variant<int, int, float, std::string> v3( 3.14f );
BOOST_TEST_EQ( v3.index(), 2 );
BOOST_TEST_EQ( get<2>(v3), 3.14f );
swap( v, v3 );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
BOOST_TEST_EQ( v3.index(), 1 );
BOOST_TEST_EQ( get<1>(v3), 1 );
variant<int, int, float, std::string> v4( 3.15f );
BOOST_TEST_EQ( v4.index(), 2 );
BOOST_TEST_EQ( get<2>(v4), 3.15f );
swap( v, v4 );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
BOOST_TEST_EQ( v4.index(), 2 );
BOOST_TEST_EQ( get<2>(v4), 3.14f );
variant<int, int, float, std::string> v5( "s1" );
BOOST_TEST_EQ( v5.index(), 3 );
BOOST_TEST_EQ( get<3>(v5), std::string("s1") );
swap( v, v5 );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
BOOST_TEST_EQ( v5.index(), 2 );
BOOST_TEST_EQ( get<2>(v5), 3.15f );
variant<int, int, float, std::string> v6( "s2" );
BOOST_TEST_EQ( v6.index(), 3 );
BOOST_TEST_EQ( get<3>(v6), std::string("s2") );
swap( v, v6 );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
BOOST_TEST_EQ( v6.index(), 3 );
BOOST_TEST_EQ( get<3>(v6), std::string("s1") );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
variant<X1, X2> v2( X1{1} );
BOOST_TEST_EQ( v2.index(), 0 );
BOOST_TEST_EQ( get<0>(v2).v, 1 );
swap( v, v2 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
BOOST_TEST_EQ( v2.index(), 0 );
BOOST_TEST_EQ( get<0>(v2).v, 0 );
variant<X1, X2> v3( in_place_index<1>, 2 );
BOOST_TEST_EQ( v3.index(), 1 );
BOOST_TEST_EQ( get<1>(v3).v, 2 );
swap( v, v3 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
BOOST_TEST_EQ( v3.index(), 0 );
BOOST_TEST_EQ( get<0>(v3).v, 1 );
variant<X1, X2> v4( in_place_index<1>, 3 );
BOOST_TEST_EQ( v4.index(), 1 );
BOOST_TEST_EQ( get<1>(v4).v, 3 );
swap( v, v4 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 3 );
BOOST_TEST_EQ( v4.index(), 1 );
BOOST_TEST_EQ( get<1>(v4).v, 2 );
variant<X1, X2> v5( in_place_index<0>, 4 );
BOOST_TEST_EQ( v5.index(), 0 );
BOOST_TEST_EQ( get<0>(v5).v, 4 );
swap( v, v5 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
BOOST_TEST_EQ( v5.index(), 1 );
BOOST_TEST_EQ( get<1>(v5).v, 3 );
}
return boost::report_errors();
}

View File

@ -0,0 +1,206 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
v = 1;
BOOST_TEST_EQ( get<0>(v), 1 );
v = 2;
BOOST_TEST_EQ( get<0>(v), 2 );
int w1 = 3;
v = w1;
BOOST_TEST_EQ( get<0>(v), 3 );
int const w2 = 4;
v = w2;
BOOST_TEST_EQ( get<0>(v), 4 );
v = std::move( w1 );
BOOST_TEST_EQ( get<0>(v), 3 );
v = std::move( w2 );
BOOST_TEST_EQ( get<0>(v), 4 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v = 1;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
v = 3.14f;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
float w1 = 3.15f;
v = w1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
int const w2 = 2;
v = w2;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 2 );
v = std::move(w1);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
v = std::move(w2);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v = 3.14f;
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
float const w1 = 3.15f;
v = w1;
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
variant<int, int, float, std::string> v5( "s1" );
v = "s1";
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
std::string w2( "s2" );
v = w2;
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
v = std::move(w1);
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
v = std::move(w2);
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v = X1{1};
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
v = X2{2};
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
X1 w1{3};
v = w1;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 3 );
X1 const w2{4};
v = w2;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
X2 w3{5};
v = w3;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 5 );
X2 const w4{6};
v = w4;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 6 );
v = std::move(w1);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 3 );
v = std::move(w2);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
return boost::report_errors();
}

View File

@ -0,0 +1,124 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
operator int() const { return 2; }
};
int main()
{
{
variant<int> v( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
}
{
variant<int> v( 'a' );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 'a' );
}
{
variant<int> v( X{} );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int const> v( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
}
{
variant<int const> v( 'a' );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 'a' );
}
{
variant<int const> v( X{} );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, float, std::string> v( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST( holds_alternative<int>(v) );
BOOST_TEST_EQ( get<0>(v), 1 );
}
{
variant<int, float, std::string> v( 'a' );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST( holds_alternative<int>(v) );
BOOST_TEST_EQ( get<0>(v), 'a' );
}
{
variant<int, float, std::string> v( X{} );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST( holds_alternative<int>(v) );
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, float, std::string> v( 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST( holds_alternative<float>(v) );
BOOST_TEST_EQ( get<1>(v), 3.14f );
}
{
variant<int, float, std::string> v( "text" );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST( holds_alternative<std::string>(v) );
BOOST_TEST_EQ( get<2>(v), std::string("text") );
}
{
variant<int, int, float, std::string> v( 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST( holds_alternative<float>(v) );
BOOST_TEST_EQ( get<2>(v), 3.14f );
}
{
variant<int, int, float, std::string> v( "text" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST( holds_alternative<std::string>(v) );
BOOST_TEST_EQ( get<3>(v), std::string("text") );
}
return boost::report_errors();
}

View File

@ -0,0 +1,109 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
struct X
{
constexpr operator int() const { return 2; }
};
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v( 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
}
{
constexpr variant<int> v( 'a' );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 'a' );
}
{
constexpr variant<int> v( X{} );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 2 );
}
{
constexpr variant<int const> v( 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
}
{
constexpr variant<int const> v( 'a' );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 'a' );
}
{
constexpr variant<int const> v( X{} );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 2 );
}
{
constexpr variant<int, float, X> v( 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( holds_alternative<int>(v) );
STATIC_ASSERT( get<0>(v) == 1 );
}
{
constexpr variant<int, float, X> v( 'a' );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( holds_alternative<int>(v) );
STATIC_ASSERT( get<0>(v) == 'a' );
}
{
constexpr variant<int, float, X> v( 3.14f );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( holds_alternative<float>(v) );
STATIC_ASSERT( get<1>(v) == 3.14f );
}
{
constexpr variant<int, float, X> v( X{} );
STATIC_ASSERT( v.index() == 2 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
{
constexpr variant<int, int, float, X> v( 3.14f );
STATIC_ASSERT( v.index() == 2 );
STATIC_ASSERT( holds_alternative<float>(v) );
STATIC_ASSERT( get<2>(v) == 3.14f );
}
{
constexpr variant<int, int, float, X> v( X{} );
STATIC_ASSERT( v.index() == 3 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
}

91
test/variant_visit.cpp Normal file
View File

@ -0,0 +1,91 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
#include <cstdio>
using namespace boost::variant2;
int main()
{
{
variant<int> v( 1 );
BOOST_TEST_EQ( (visit( []( auto x ){ return x; }, v )), 1 );
visit( []( auto x ){ BOOST_TEST_EQ( x, 1 ); }, v );
visit( []( auto x ){ BOOST_TEST_EQ( x, 1 ); }, std::move(v) );
}
{
variant<int> const v( 2 );
BOOST_TEST_EQ( (visit( []( auto x ){ return x; }, v )), 2 );
visit( []( auto x ){ BOOST_TEST_EQ( x, 2 ); }, v );
visit( []( auto x ){ BOOST_TEST_EQ( x, 2 ); }, std::move(v) );
}
{
variant<int const> v( 3 );
BOOST_TEST_EQ( (visit( []( auto x ){ return x; }, v )), 3 );
visit( []( auto x ){ BOOST_TEST_EQ( x, 3 ); }, v );
visit( []( auto x ){ BOOST_TEST_EQ( x, 3 ); }, std::move(v) );
}
{
variant<int const> const v( 4 );
BOOST_TEST_EQ( (visit( []( auto x ){ return x; }, v )), 4 );
visit( []( auto x ){ BOOST_TEST_EQ( x, 4 ); }, v );
visit( []( auto x ){ BOOST_TEST_EQ( x, 4 ); }, std::move(v) );
}
{
variant<int, float> v1( 1 );
variant<int, float> const v2( 3.14f );
BOOST_TEST_EQ( (visit( []( auto x1, auto x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 );
visit( []( auto x1, auto x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 );
visit( []( auto x1, auto x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, std::move(v1), std::move(v2) );
}
{
variant<int, float, double> v1( 1 );
variant<int, float, double> const v2( 3.14f );
variant<int, float, double> v3( 6.28 );
BOOST_TEST_EQ( (visit( []( auto x1, auto x2, auto x3 ){ return (int)(x1 * 100) * 1000000 + (int)(x2 * 100) * 1000 + (int)(x3 * 100); }, v1, v2, v3 )), 100314628 );
visit( []( auto x1, auto x2, auto x3 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); }, v1, v2, v3 );
visit( []( auto x1, auto x2, auto x3 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); }, std::move(v1), std::move(v2), std::move(v3) );
}
{
variant<int, float, double, char> v1( 1 );
variant<int, float, double, char> const v2( 3.14f );
variant<int, float, double, char> v3( 6.28 );
variant<int, float, double, char> const v4( 'A' );
BOOST_TEST_EQ( (visit( []( auto x1, auto x2, auto x3, auto x4 ){ return (long long)(x1 * 100) * 100000000 + (long long)(x2 * 100) * 100000 + (long long)(x3 * 10000) + (int)x4; }, v1, v2, v3, v4 )), 10031462800 + 'A' );
visit( []( auto x1, auto x2, auto x3, auto x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, v1, v2, v3, v4 );
visit( []( auto x1, auto x2, auto x3, auto x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, std::move(v1), std::move(v2), std::move(v3), std::move(v4) );
}
return boost::report_errors();
}