Clang 找不到转换运算符
Clang Cannot Find Conversion Operator
在以下代码中,clang 抱怨不存在转换运算符:
class A{
public:
operator int () const {return i;}
A(int i_): i(i_) {}
template<class B>
friend auto operator * (const B& l, A&& r)
-> decltype(l * int(std::move(r))){
return l * int(std::move(r));
}
int i;
};
所有 clang 版本 6-9 都会发生这种情况,请参阅 https://godbolt.org/z/ELuRHp。报错信息如下:
<source>:11:19: error: cannot convert 'typename std::remove_reference<A &>::type' (aka 'A') to
'int' without a conversion operator
-> decltype(l * int(std::move(r))){
^~~~~~~~~~~~~~~~
相同的代码用 gcc 7-9 编译得很好,所以我想知道这是 clang 中的错误还是代码不正确。
编辑:@daniel-langr 提供的同一错误的较短版本可在此处找到 https://godbolt.org/z/Krnh27。
原始代码示例是我们代码库中 class 的简化版本,因此不需要 decltype
.
这看起来像是一个 clang 错误。
For a name used anywhere in class definition (including base class specifiers and nested class definitions), except inside a member function body, a default argument of a member function, exception specification of a member function, or default member initializer, where the member may belong to a nested class whose definition is in the body of the enclosing class, the following scopes are searched:
a) the body of the class in which the name is used until the point of use
...
根据以上所述,您的用户定义的转换函数必须在尾部 return 类型中可见。
这是在 gcc 上运行良好但在 clang 上无法编译的成员函数的另一个示例。
void f(A a, decltype(int(A)) {}
在以下代码中,clang 抱怨不存在转换运算符:
class A{
public:
operator int () const {return i;}
A(int i_): i(i_) {}
template<class B>
friend auto operator * (const B& l, A&& r)
-> decltype(l * int(std::move(r))){
return l * int(std::move(r));
}
int i;
};
所有 clang 版本 6-9 都会发生这种情况,请参阅 https://godbolt.org/z/ELuRHp。报错信息如下:
<source>:11:19: error: cannot convert 'typename std::remove_reference<A &>::type' (aka 'A') to
'int' without a conversion operator
-> decltype(l * int(std::move(r))){
^~~~~~~~~~~~~~~~
相同的代码用 gcc 7-9 编译得很好,所以我想知道这是 clang 中的错误还是代码不正确。
编辑:@daniel-langr 提供的同一错误的较短版本可在此处找到 https://godbolt.org/z/Krnh27。
原始代码示例是我们代码库中 class 的简化版本,因此不需要 decltype
.
这看起来像是一个 clang 错误。
For a name used anywhere in class definition (including base class specifiers and nested class definitions), except inside a member function body, a default argument of a member function, exception specification of a member function, or default member initializer, where the member may belong to a nested class whose definition is in the body of the enclosing class, the following scopes are searched:
a) the body of the class in which the name is used until the point of use
...
根据以上所述,您的用户定义的转换函数必须在尾部 return 类型中可见。
这是在 gcc 上运行良好但在 clang 上无法编译的成员函数的另一个示例。
void f(A a, decltype(int(A)) {}