From 781977a23751c6a9ec07a387fc54c32cf5190e47 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 29 May 2017 21:38:04 +0300 Subject: [PATCH] Add test/get_by_index.cpp --- include/boost/variant2/variant.hpp | 44 +------ test/Jamfile | 1 + test/get_by_index.cpp | 184 +++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 40 deletions(-) create mode 100644 test/get_by_index.cpp diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 29991c1..28079b6 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -114,61 +114,25 @@ template constexpr bool holds_alternative( variant co template constexpr variant_alternative_t>& get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); - -#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) - - return v.index() == I? v._get_impl( mp_size_t() ): ( throw bad_variant_access(), v._get_impl( mp_size_t() ) ); - -#else - - return v.index() == I? v._get_impl( mp_size_t() ): throw bad_variant_access(); - -#endif + return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t() ); } template constexpr variant_alternative_t>&& get(variant&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); - -#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) - - return std::move( v.index() == I? v._get_impl( mp_size_t() ): ( throw bad_variant_access(), v._get_impl( mp_size_t() ) ) ); - -#else - - return v.index() == I? std::move( v._get_impl( mp_size_t() ) ): throw bad_variant_access(); - -#endif + return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t() ) ); } template constexpr variant_alternative_t const>& get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); - -#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) - - return v.index() == I? v._get_impl( mp_size_t() ): ( throw bad_variant_access(), v._get_impl( mp_size_t() ) ); - -#else - - return v.index() == I? v._get_impl( mp_size_t() ): throw bad_variant_access(); - -#endif + return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t() ); } template constexpr variant_alternative_t const>&& get(variant const&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); - -#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 ) - - return std::move( v.index() == I? v._get_impl( mp_size_t() ): ( throw bad_variant_access(), v._get_impl( mp_size_t() ) ) ); - -#else - - return v.index() == I? std::move( v._get_impl( mp_size_t() ) ): throw bad_variant_access(); - -#endif + return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t() ) ); } // get diff --git a/test/Jamfile b/test/Jamfile index 766c254..3a35203 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -15,3 +15,4 @@ run variant_size.cpp : : : $(REQ) ; run variant_alternative.cpp : : : $(REQ) ; run holds_alternative.cpp : : : $(REQ) ; run holds_alternative_cx.cpp : : : $(REQ) ; +run get_by_index.cpp : : : $(REQ) ; diff --git a/test/get_by_index.cpp b/test/get_by_index.cpp new file mode 100644 index 0000000..7491d9b --- /dev/null +++ b/test/get_by_index.cpp @@ -0,0 +1,184 @@ + +// 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<0>(v), 0 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) ); + } + + { + variant v( 1 ); + + BOOST_TEST_EQ( get<0>(v), 1 ); + BOOST_TEST_EQ( get_if<0>(&v), &get<0>(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<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 ); + } + + { + variant 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 ); + } + + { + variant 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) ); + } + + { + variant 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 ); + } + + { + variant 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 ); + } + + { + variant 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) ); + } + + return boost::report_errors(); +}