Boost logo

Boost :

Subject: Re: [boost] variant - off topic
From: degski (degski_at_[hidden])
Date: 2019-04-08 08:02:07


On Mon, 8 Apr 2019 at 07:45, degski <degski_at_[hidden]> wrote:

> Just now I'm at it:
>

Well, a bit too quick maybe:

#include <type_traits>
#include <variant>

#include <boost/mp11.hpp>
#include <boost/variant.hpp>

namespace mp11 = boost::mp11;

struct Foo {
    Foo ( ) = delete;
};

struct Bar {
    Bar & operator= ( const Bar & ) = 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 nothrow constructible or not nothrow 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 nothrow constructible or not nothrow 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
    safe_std_variant<int, double, bool, Bar> sv9; // does compile
    safe_boost_variant<int, double, bool, Bar> sv10; // does compile
    evensafer_std_variant<int, double, bool, Bar> sv11; // does not
compile
    evensafer_boost_variant<int, double, bool, Bar> sv12; // 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