小编Mr.*_*ric的帖子

当重载具有多重继承的函数时,GCC称调用它是不明确的,但Clang和MSVC不这样做

我正在使用这个变体库: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)

c++ g++ clang++ c++17

7
推荐指数
1
解决办法
233
查看次数

标签 统计

c++ ×1

c++17 ×1

clang++ ×1

g++ ×1