我正在使用这个变体库:https://github.com/cbeck88/strict-variant.它提供了一个类似于std::variant和的类boost::variant.鉴于此struct:
struct S
{
explicit S(double) {}
};
Run Code Online (Sandbox Code Playgroud)
我想做这个:
strict_variant::variant<double, S> v = 2.0;
Run Code Online (Sandbox Code Playgroud)
这适用于Clang 5.0.1和MSVC 19.12.25831.00,但无法使用GCC 7.2.1进行编译.
我查看了库的代码并将问题减少到了:
#include <iostream>
struct S
{
constexpr S() {}
constexpr explicit S(double) {}
};
template<unsigned i> struct init_helper;
template<> struct init_helper<0> { using type = double; };
template<> struct init_helper<1> { using type = S; };
template<unsigned i>
struct initializer_leaf
{
using target_type = typename init_helper<i>::type;
constexpr unsigned operator()(target_type) const
{
return …Run Code Online (Sandbox Code Playgroud)