涉及模板化转换操作符和隐式复制构造函数的模糊性
Ambiguity involving templated conversion operator and implicit copy constructor
clang和GCC在以下代码中的行为不同:
struct foo
{
foo(int);
};
struct waldo
{
template <typename T>
operator T();
};
int main()
{
waldo w;
foo f{w};
}
clang接受此代码,并调用foo(int)
构造函数。然而,gcc抱怨foo(int)
构造函数和隐式生成的复制和移动构造函数之间存在歧义:
test.cpp: In function 'int main()':
test.cpp:15:12: error: call of overloaded 'foo(<brace-enclosed initializer list>)' is ambiguous
foo f{w};
^
test.cpp:15:12: note: candidates are:
test.cpp:3:5: note: foo::foo(int)
foo(int);
^
test.cpp:1:8: note: constexpr foo::foo(const foo&)
struct foo
^
test.cpp:1:8: note: constexpr foo::foo(foo&&)
谁对?
还值得注意的是,如果将foo f{w}
更改为foo f(w)
(注意从大括号更改为括号),gcc和clang都会给出错误。这让我希望gcc在上面例子中的行为(即给出错误)是正确的,否则()
和{}
初始化形式之间会出现奇怪的不一致。
EDIT:遵循Kerrek SB的建议,我尝试delete
复制foo
的构造函数:
struct foo
{
foo(int);
foo(const foo&) = delete;
};
行为保持不变
对于列表初始化,如果列表的元素有一个元素(这里是w
),并且考虑带形参"reference to const/volatile X"的类X
的构造函数,则不考虑用户定义的转换。因此,foo
的复制和移动构造函数都不能使用。因此,毫无疑问地选择了foo(int)
构造函数。
所以Clang是正确的。
编辑:对于这里的标准人员,参见13.3.3.1p4
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用