Add variant<>::uses_double_storage(). Refs #37.

This commit is contained in:
Peter Dimov
2023-06-28 22:10:43 +03:00
parent 95a8c5ffec
commit 26595285d3
3 changed files with 74 additions and 0 deletions

View File

@ -920,6 +920,11 @@ template<class... T> struct variant_base_impl<true, true, T...>
this->emplace_impl<J, U>( std::is_nothrow_constructible<U, A&&...>(), std::forward<A>(a)... );
}
static constexpr bool uses_double_storage() noexcept
{
return false;
}
};
// trivially destructible, double buffered
@ -978,6 +983,11 @@ template<class... T> struct variant_base_impl<true, false, T...>
ix_ = J * 2 + i2;
}
static constexpr bool uses_double_storage() noexcept
{
return true;
}
};
// not trivially destructible, single buffered
@ -1068,6 +1078,11 @@ template<class... T> struct variant_base_impl<false, true, T...>
st_.emplace( mp11::mp_size_t<J>(), std::move(tmp) );
ix_ = J;
}
static constexpr bool uses_double_storage() noexcept
{
return false;
}
};
// not trivially destructible, double buffered
@ -1193,6 +1208,11 @@ template<class... T> struct variant_base_impl<false, false, T...>
ix_ = J * 2 + i2;
}
static constexpr bool uses_double_storage() noexcept
{
return true;
}
};
} // namespace detail
@ -1703,6 +1723,8 @@ public:
using variant_base::index;
using variant_base::uses_double_storage;
// swap
private:

View File

@ -131,3 +131,5 @@ local JSON = <library>/boost//json/<warnings>off "<toolset>msvc-14.0:<build>no"
run variant_json_value_from.cpp : : : $(JSON) ;
run variant_json_value_to.cpp : : : $(JSON) ;
compile variant_uses_double_storage.cpp ;

View File

@ -0,0 +1,50 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <type_traits>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
};
STATIC_ASSERT( std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( std::is_trivially_destructible<X1>::value );
struct X2
{
~X2() {}
};
STATIC_ASSERT( std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_trivially_destructible<X2>::value );
struct X3
{
X3( X3&& ) {}
};
STATIC_ASSERT( !std::is_nothrow_move_constructible<X3>::value );
STATIC_ASSERT( std::is_trivially_destructible<X3>::value );
struct X4
{
~X4() {}
X4( X4&& ) {}
};
STATIC_ASSERT( !std::is_nothrow_move_constructible<X4>::value );
STATIC_ASSERT( !std::is_trivially_destructible<X4>::value );
//
STATIC_ASSERT( !variant<int, float>::uses_double_storage() );
STATIC_ASSERT( !variant<int, float, X1>::uses_double_storage() );
STATIC_ASSERT( !variant<int, float, X2>::uses_double_storage() );
STATIC_ASSERT( variant<int, float, X3>::uses_double_storage() );
STATIC_ASSERT( variant<int, float, X4>::uses_double_storage() );