相关疑难解决方法(0)

如何在模板化转换运算符中消除歧义?

在混淆之后为什么我的代码在GCC上给了我一个模糊错误但在Clang上没有错误,我简化了代码.它可以在下面看到.

struct Foo
{
    // Foo(Foo&&) = delete;
    // Foo(const Foo&) = delete;
    Foo(int*) {}
};

struct Bar
{    
    template<typename T>
    operator T()
    {
        return Foo{nullptr};
    }
};

int main() { Foo f{Bar{}}; }
Run Code Online (Sandbox Code Playgroud)

错误如下.

main.cpp:17:18: error: call to constructor of 'Foo' is ambiguous
int main() { Foo f{Bar{}}; }
                 ^~~~~~~~
main.cpp:1:8: note: candidate is the implicit move constructor
struct Foo
       ^
main.cpp:1:8: note: candidate is the implicit copy constructor
main.cpp:5:1: note: candidate constructor
Foo(int*) {}
^
Run Code Online (Sandbox Code Playgroud)

这次我无法让它成功地为Clang编译,所以我想这只是一个Clang bug而且这是预期的行为.

当我明确删除副本并移动构造函数(即取消注释前两行代码)时,我得到了 …

c++ templates type-conversion ambiguous

12
推荐指数
1
解决办法
313
查看次数

标签 统计

ambiguous ×1

c++ ×1

templates ×1

type-conversion ×1