|
Boost : |
Subject: Re: [boost] variant - off topic
From: degski (degski_at_[hidden])
Date: 2019-04-08 07:45:48
On Mon, 8 Apr 2019 at 07:30, Gavin Lambert via Boost <boost_at_[hidden]>
wrote:
> Or did you mean to check is_nothrow_constructible rather than
> is_trivially_constructible?
>
> (Also, for robustness you should check assignment as well as construction.)
>
Just now I'm at it:
#include <type_traits>
#include <variant>
#include <boost/mp11.hpp>
#include <boost/variant.hpp>
namespace mp11 = boost::mp11;
struct Foo {
Foo ( ) = delete;
};
template<typename... Args>
struct safe_std_variant : public std::variant<Args...> {
static_assert (
std::conjunction<std::is_trivially_constructible<Args>...>::value, "is not
trivially constructible" );
};
template<typename... Args>
struct safe_boost_variant : public boost::variant<Args...> {
static_assert (
mp11::mp_all<std::is_trivially_constructible<Args>...>::value, "is not
trivially constructible" );
};
template<typename... Args>
struct evensafer_std_variant : public std::variant<Args...> {
static_assert (
std::conjunction<std::conjunction<std::is_nothrow_constructible<Args>...>,
std::conjunction<std::is_nothrow_copy_assignable<Args>...>>::value,
"is not trivially constructible or copy assignable" );
};
template<typename... Args>
struct evensafer_boost_variant : public boost::variant<Args...> {
static_assert (
mp11::mp_all<mp11::mp_all<std::is_nothrow_constructible<Args>...>,
mp11::mp_all<std::is_nothrow_copy_assignable<Args>...>>::value,
"is not trivially constructible or copy assignable" );
};
int main ( ) {
safe_std_variant<int, double, bool> sv1; // compiles
safe_boost_variant<int, double, bool> sv2; // compiles
evensafer_std_variant<int, double, bool> sv3; // compiles
evensafer_boost_variant<int, double, bool> sv4; // compiles
// safe_std_variant<int, double, bool, Foo> sv5; // does not
compile
// safe_boost_variant<int, double, bool, Foo> sv6; // does not
compile
// evensafer_std_variant<int, double, bool, Foo> sv7; // does not
compile
// evensafer_boost_variant<int, double, bool, Foo> sv8; // does not
compile
}
degski
-- *Microsoft, please kill Paint3D*
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk