constexpr the copy/move constructors; change copy/move assign to not require constexpr lambdas

This commit is contained in:
Peter Dimov
2017-06-09 17:24:23 +03:00
parent f216bb5c1f
commit 82104cd55e
6 changed files with 291 additions and 25 deletions

View File

@ -868,8 +868,19 @@ public:
{
}
template<class E1 = void, class E2 = mp_if<mp_all<std::is_copy_constructible<T>...>, E1>>
constexpr variant( variant const& r )
template<class E1 = void,
class E2 = mp_if<mp_all<std::is_trivially_copy_constructible<T>...>, E1>
>
constexpr variant( variant const& r ) noexcept
: variant_base( static_cast<variant_base const&>(r) )
{
}
template<class E1 = void,
class E2 = mp_if<mp_not<mp_all<std::is_trivially_copy_constructible<T>...>>, E1>,
class E3 = mp_if<mp_all<std::is_copy_constructible<T>...>, E1>
>
variant( variant const& r )
noexcept( mp_all<std::is_nothrow_copy_constructible<T>...>::value )
{
mp_with_index<sizeof...(T)>( r.index(), [&]( auto I ){
@ -879,8 +890,19 @@ public:
});
}
template<class E1 = void, class E2 = mp_if<mp_all<std::is_move_constructible<T>...>, E1>>
constexpr variant( variant && r )
template<class E1 = void,
class E2 = mp_if<mp_all<std::is_trivially_move_constructible<T>...>, E1>
>
constexpr variant( variant && r ) noexcept
: variant_base( static_cast<variant_base&&>(r) )
{
}
template<class E1 = void,
class E2 = mp_if<mp_not<mp_all<std::is_trivially_move_constructible<T>...>>, E1>,
class E3 = mp_if<mp_all<std::is_move_constructible<T>...>, E1>
>
variant( variant && r )
noexcept( mp_all<std::is_nothrow_move_constructible<T>...>::value )
{
mp_with_index<sizeof...(T)>( r.index(), [&]( auto I ){
@ -923,7 +945,19 @@ public:
}
// assignment
template<class E1 = void, class E2 = mp_if<mp_all<std::is_copy_constructible<T>..., std::is_copy_assignable<T>...>, E1>>
template<class E1 = void,
class E2 = mp_if<mp_all<std::is_trivially_destructible<T>..., std::is_trivially_copy_assignable<T>...>, E1>
>
constexpr variant& operator=( variant const & r ) noexcept
{
static_cast<variant_base&>( *this ) = static_cast<variant_base const&>( r );
return *this;
}
template<class E1 = void,
class E2 = mp_if<mp_not<mp_all<std::is_trivially_destructible<T>..., std::is_trivially_copy_assignable<T>...>>, E1>,
class E3 = mp_if<mp_all<std::is_copy_constructible<T>..., std::is_copy_assignable<T>...>, E1>
>
constexpr variant& operator=( variant const & r )
noexcept( mp_all<std::is_nothrow_copy_constructible<T>..., std::is_nothrow_copy_assignable<T>...>::value )
{
@ -943,8 +977,20 @@ public:
return *this;
}
template<class E1 = void, class E2 = mp_if<mp_all<std::is_move_constructible<T>..., std::is_move_assignable<T>...>, E1>>
constexpr variant& operator=( variant && r )
template<class E1 = void,
class E2 = mp_if<mp_all<std::is_trivially_destructible<T>..., std::is_trivially_move_assignable<T>...>, E1>
>
constexpr variant& operator=( variant && r ) noexcept
{
static_cast<variant_base&>( *this ) = static_cast<variant_base&&>( r );
return *this;
}
template<class E1 = void,
class E2 = mp_if<mp_not<mp_all<std::is_trivially_destructible<T>..., std::is_trivially_move_assignable<T>...>>, E1>,
class E3 = mp_if<mp_all<std::is_move_constructible<T>..., std::is_move_assignable<T>...>, E1>
>
variant& operator=( variant && r )
noexcept( mp_all<std::is_nothrow_move_constructible<T>..., std::is_nothrow_move_assignable<T>...>::value )
{
mp_with_index<sizeof...(T)>( r.index(), [&]( auto I ){

View File

@ -27,7 +27,10 @@ run variant_default_construct.cpp : : : $(REQ) ;
compile variant_default_construct_cx.cpp : : : $(REQ) ;
run variant_copy_construct.cpp : : : $(REQ) ;
compile variant_copy_construct_cx.cpp : : : $(REQ) ;
run variant_move_construct.cpp : : : $(REQ) ;
compile variant_move_construct_cx.cpp : : : $(REQ) ;
run variant_value_construct.cpp : : : $(REQ) ;
compile variant_value_construct_cx.cpp : : : $(REQ) ;

View File

@ -6,13 +6,6 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if !defined( __cpp_constexpr ) || __cpp_constexpr < 201603
// no constexpr lambda support
int main() {}
#else
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
@ -121,5 +114,3 @@ int main()
#endif
}
#endif // constexpr lambda support

View File

@ -0,0 +1,122 @@
// 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
#if !defined( __cpp_constexpr ) || __cpp_constexpr < 201603
// no constexpr lambda support
int main() {}
#else
#include <boost/variant2/variant.hpp>
using namespace boost::variant2;
struct X
{
int v;
X() = default;
constexpr X( int v ): v( v ) {}
constexpr operator int() const { return v; }
};
struct Y
{
int v;
constexpr Y(): v() {}
constexpr Y( int v ): v( v ) {}
constexpr operator int() const { return v; }
};
enum E
{
v
};
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class T, class V> constexpr T test( V const& v )
{
V v2( v );
return get<T>(v);
}
int main()
{
{
constexpr variant<int> v( 1 );
constexpr auto w = test<int>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<X> v( 1 );
constexpr auto w = test<X>( v );
STATIC_ASSERT( w == 1 );
}
#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
#else
{
constexpr variant<Y> v( 1 );
constexpr auto w = test<Y>( v );
STATIC_ASSERT( w == 1 );
}
#endif
{
constexpr variant<int, float> v( 1 );
constexpr auto w = test<int>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, float> v( 3.0f );
constexpr auto w = test<float>( v );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr variant<int, int, float> v( 3.0f );
constexpr auto w = test<float>( v );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr variant<E, E, X> v( 1 );
constexpr auto w = test<X>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, int, float, float, X> v( X(1) );
constexpr auto w = test<X>( v );
STATIC_ASSERT( w == 1 );
}
#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
#else
{
constexpr variant<E, E, Y> v( 1 );
constexpr auto w = test<Y>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, int, float, float, Y> v( Y(1) );
constexpr auto w = test<Y>( v );
STATIC_ASSERT( w == 1 );
}
#endif
}
#endif // constexpr lambda support

View File

@ -6,13 +6,6 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#if !defined( __cpp_constexpr ) || __cpp_constexpr < 201603
// no constexpr lambda support
int main() {}
#else
#include <boost/variant2/variant.hpp>
#include <utility>
@ -112,5 +105,3 @@ int main()
#endif
}
#endif // constexpr lambda support

View File

@ -0,0 +1,113 @@
// 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
#if !defined( __cpp_constexpr ) || __cpp_constexpr < 201603
// no constexpr lambda support
int main() {}
#else
#include <boost/variant2/variant.hpp>
#include <utility>
using namespace boost::variant2;
struct X
{
int v;
X() = default;
constexpr X( int v ): v( v ) {}
constexpr operator int() const { return v; }
};
struct Y
{
int v;
constexpr Y(): v() {}
constexpr Y( int v ): v( v ) {}
constexpr operator int() const { return v; }
};
enum E
{
v
};
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class T, class V> constexpr T test( V&& v )
{
V v2( std::forward<V>(v) );
return get<T>(v);
}
int main()
{
{
constexpr auto w = test<int>( variant<int>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<X>( variant<X>( 1 ) );
STATIC_ASSERT( w == 1 );
}
#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
#else
{
constexpr auto w = test<Y>( variant<Y>( 1 ) );
STATIC_ASSERT( w == 1 );
}
#endif
{
constexpr auto w = test<int>( variant<int, float>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<float>( variant<int, float>( 3.0f ) );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<float>( variant<int, int, float>( 3.0f ) );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<X>( variant<E, E, X>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<X>( variant<int, int, float, float, X>( X(1) ) );
STATIC_ASSERT( w == 1 );
}
#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
#else
{
constexpr auto w = test<Y>( variant<E, E, Y>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<Y>( variant<int, int, float, float, Y>( Y(1) ) );
STATIC_ASSERT( w == 1 );
}
#endif
}
#endif // constexpr lambda support