diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 28079b6..fbd2628 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -142,7 +142,7 @@ template constexpr U& get(variant& v) static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; - return v.index() == I? v._get_impl( mp_size_t() ): throw bad_variant_access(); + return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t() ); } template constexpr U&& get(variant&& v) @@ -150,7 +150,7 @@ template constexpr U&& get(variant&& v) static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; - return v.index() == I? std::move( v._get_impl( mp_size_t() ) ): throw bad_variant_access(); + return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t() ) ); } template constexpr U const& get(variant const& v) @@ -158,7 +158,7 @@ template constexpr U const& get(variant const& v) static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; - return v.index() == I? v._get_impl( mp_size_t() ): throw bad_variant_access(); + return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t() ); } template constexpr U const&& get(variant const&& v) @@ -166,7 +166,7 @@ template constexpr U const&& get(variant const&& v) static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; - return v.index() == I? std::move( v._get_impl( mp_size_t() ) ): throw bad_variant_access(); + return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t() ) ); } // get_if diff --git a/test/Jamfile b/test/Jamfile index 3a35203..78a822a 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -14,5 +14,8 @@ REQ = ; #[ requires cxx11_variadic_templates cxx11_template_aliases cxx11_declty run variant_size.cpp : : : $(REQ) ; run variant_alternative.cpp : : : $(REQ) ; run holds_alternative.cpp : : : $(REQ) ; -run holds_alternative_cx.cpp : : : $(REQ) ; +compile holds_alternative_cx.cpp : : : $(REQ) ; run get_by_index.cpp : : : $(REQ) ; +compile get_by_index_cx.cpp : : : $(REQ) ; +run get_by_type.cpp : : : $(REQ) ; +compile get_by_type_cx.cpp : : : $(REQ) ; diff --git a/test/get_by_index_cx.cpp b/test/get_by_index_cx.cpp new file mode 100644 index 0000000..ae1b99f --- /dev/null +++ b/test/get_by_index_cx.cpp @@ -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 + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v; + + STATIC_ASSERT( get<0>(v) == 0 ); + STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get<0>(v) == 1 ); + STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) ); + } + + { + constexpr variant 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 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 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 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 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 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) ); + } +} diff --git a/test/get_by_type.cpp b/test/get_by_type.cpp new file mode 100644 index 0000000..82b18c7 --- /dev/null +++ b/test/get_by_type.cpp @@ -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 +#include +#include +#include +#include +#include + +using namespace boost::variant2; + +int main() +{ + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const volatile*>)); + } + + { + variant v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float const*>)); + } + + { + variant v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float volatile*>)); + } + + { + variant const v; + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), int const&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), int const&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), int const*>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same(v)), float const volatile&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(std::move(v))), float const volatile&&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same(&v)), float const volatile*>)); + } + + { + variant v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_THROWS( get(v), bad_variant_access ); + BOOST_TEST_EQ( get_if(&v), nullptr ); + + BOOST_TEST_EQ( get(v), 3.14f ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + } + + { + variant v; + + BOOST_TEST_EQ( get(v), 0 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get(v), 1 ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + } + + { + variant v( 3.14f ); + + BOOST_TEST_EQ( get(v), 3.14f ); + BOOST_TEST_EQ( get_if(&v), &get(v) ); + } + + return boost::report_errors(); +} diff --git a/test/get_by_type_cx.cpp b/test/get_by_type_cx.cpp new file mode 100644 index 0000000..b4995fe --- /dev/null +++ b/test/get_by_type_cx.cpp @@ -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 + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v; + + STATIC_ASSERT( get(v) == 0 ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get(v) == 1 ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + } + + { + constexpr variant v; + + STATIC_ASSERT( get(v) == 0 ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + + STATIC_ASSERT( get_if(&v) == nullptr ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get(v) == 1 ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + + STATIC_ASSERT( get_if(&v) == nullptr ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( get_if(&v) == nullptr ); + + STATIC_ASSERT( get(v) == 3.14f ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + } + + { + constexpr variant v; + + STATIC_ASSERT( get(v) == 0 ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + } + + { + constexpr variant v( 1 ); + + STATIC_ASSERT( get(v) == 1 ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + } + + { + constexpr variant v( 3.14f ); + + STATIC_ASSERT( get(v) == 3.14f ); + STATIC_ASSERT( get_if(&v) == &get(v) ); + } +}