From 26595285d36be6b506e8888283c2b0da30778d70 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 28 Jun 2023 22:10:43 +0300 Subject: [PATCH] Add variant<>::uses_double_storage(). Refs #37. --- include/boost/variant2/variant.hpp | 22 ++++++++++++ test/Jamfile | 2 ++ test/variant_uses_double_storage.cpp | 50 ++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 test/variant_uses_double_storage.cpp diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 7451eda..f0e8fef 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -920,6 +920,11 @@ template struct variant_base_impl this->emplace_impl( std::is_nothrow_constructible(), std::forward(a)... ); } + + static constexpr bool uses_double_storage() noexcept + { + return false; + } }; // trivially destructible, double buffered @@ -978,6 +983,11 @@ template struct variant_base_impl ix_ = J * 2 + i2; } + + static constexpr bool uses_double_storage() noexcept + { + return true; + } }; // not trivially destructible, single buffered @@ -1068,6 +1078,11 @@ template struct variant_base_impl st_.emplace( mp11::mp_size_t(), std::move(tmp) ); ix_ = J; } + + static constexpr bool uses_double_storage() noexcept + { + return false; + } }; // not trivially destructible, double buffered @@ -1193,6 +1208,11 @@ template struct variant_base_impl 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: diff --git a/test/Jamfile b/test/Jamfile index 3c67277..f843257 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -131,3 +131,5 @@ local JSON = /boost//json/off "msvc-14.0:no" run variant_json_value_from.cpp : : : $(JSON) ; run variant_json_value_to.cpp : : : $(JSON) ; + +compile variant_uses_double_storage.cpp ; diff --git a/test/variant_uses_double_storage.cpp b/test/variant_uses_double_storage.cpp new file mode 100644 index 0000000..d78efc6 --- /dev/null +++ b/test/variant_uses_double_storage.cpp @@ -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 +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ +}; + +STATIC_ASSERT( std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( std::is_trivially_destructible::value ); + +struct X2 +{ + ~X2() {} +}; + +STATIC_ASSERT( std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_trivially_destructible::value ); + +struct X3 +{ + X3( X3&& ) {} +}; + +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( std::is_trivially_destructible::value ); + +struct X4 +{ + ~X4() {} + X4( X4&& ) {} +}; + +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_trivially_destructible::value ); + +// + +STATIC_ASSERT( !variant::uses_double_storage() ); +STATIC_ASSERT( !variant::uses_double_storage() ); +STATIC_ASSERT( !variant::uses_double_storage() ); +STATIC_ASSERT( variant::uses_double_storage() ); +STATIC_ASSERT( variant::uses_double_storage() );