gpt4 book ai didi

c++ - GCC C++ 错误? (T)x 匹配 X::operator const T&() const 与 clang 的不同

转载 作者:行者123 更新时间:2023-12-01 13:47:58 27 4
gpt4 key购买 nike

我遇到了 GCC 和 clang(在 Godbolt 测试的大范围版本 - 都有相同的差异)与转换运算符匹配之间的差异。现在使用 Barry 更短的复制代码 - 也可在 godbolt 获得.

struct X {
template <typename T>
operator T const&() const;
};

int i = X();

operator T const&() const转换为 int 时匹配(clang 说是/GCC 需要 operator T() const 来避免:
<source>:6:9: error: cannot convert 'X' to 'int' in initialization
6 | int i = X();
| ^~~

C++17 标准的相关部分是 15.3 转换 [class.conv] .其中,15.3.5 说...

Function overload resolution (16.3.3) selects the best conversion function to perform the conversion.



...这几乎是最重要的。 16.3.3 是 的部分最佳可行函数 [over.match.best] ,但通常组合使用 16.3.2 可行功能 [over.match.viable] .该标准似乎没有明确说明 16.3.2 是否适用于此,但假设它适用,我们在 16.3.2.1 中有:

From the set of candidate functions constructed for a given context (16.3.1), a set of viable functions is chosen



这就提出了 GCC 是否考虑 operator const T&() const 的问题一个不可行的候选人,或者甚至不是一个候选人。候选人有效的要求非常简单:正确数量的参数,并且“对于每个参数都应该存在一个隐式转换序列(16.3.3.1),该序列将该参数转换为相应的参数......”,其中这里显然是真的,所以我相信 GCC 一定不要考虑 operator const T&() const一个候选人,这将我们带到 16.3.1 候选函数和参数列表 [over.match.funcs] ,特别是 16.3.1/7:

In each case where a candidate is a function template, candidate function template specializations are generated using template argument deduction (17.8.3, 17.8.2). Those candidates are then handled as candidate functions in the usual way.



鉴于说 template <typename T> void f(const T&);可以用 int 调用参数类型,我认为 GCC 应该将转换运算符视为有效的候选者,就像 clang 一样。

虽然我很感激确认/想法。如果人们同意,我将针对 GCC 提出错误报告。

最佳答案

这是一个 GCC 错误,如 pointed out by T.C. .

关于c++ - GCC C++ 错误? (T)x 匹配 X::operator const T&() const 与 clang 的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60439839/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com