Boost logo

Boost :

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


On Mon, 8 Apr 2019 at 06:30, Robert Ramey via Boost <boost_at_[hidden]>
wrote:

> LOL - Looks good to me! So now we have std::variant that is guaranteed
> to have a valid state with no double buffer BS and no extra overhead.
>
> I'll be curious to hear what otherothers have to say about this.
>

And here's the boost version as well:

#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" );
};

int main ( ) {

    safe_std_variant<int, double, bool> sv1; // compiles
    safe_boost_variant<int, double, bool> sv2; // compiles
    safe_std_variant<int, double, bool, Foo> sv3; // does not compile
    safe_boost_variant<int, double, bool, Foo> sv4; // 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