c++ - Clang 找不到转换运算符
问题描述
在以下代码中,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。原始代码示例是我们代码库中类的简化版本,因此不需要decltype
.
解决方案
这看起来像一个clang错误。
从不合格的名称查找:
对于在类定义(包括基类说明符和嵌套类定义)中使用的名称,除了在成员函数体内部、成员函数的默认参数、成员函数的异常规范或默认成员初始值设定项,其中成员可以属于一个嵌套类,其定义在封闭类的主体中,搜索以下范围:
a) 在使用点之前使用名称的类的主体
...
根据上述,您的用户定义的转换函数必须在尾随返回类型中可见。
这是成员函数的另一个示例,它在 gcc 上运行良好,但在 clang 上无法编译。
void f(A a, decltype(int(A)) {}
推荐阅读
- c# - JsonConvert.DeserializeObject 数组
- .net - 在 AWS X-RAY SDK .NET 中动态创建分段
- enums - 如果我们遵循枚举本地化约定,ABP 可以在哪些情况下自动本地化枚举
- spring-boot - 无法显示会话的自定义消息[SPRING_SECURITY_LAST_EXCEPTION]
- python - 当多个用户连接到应用程序时,Web 服务非常慢
- php - Laravel 错误:在 Excel 导入中从空值创建默认对象
- virtuoso - Virtuoso 安装在第二次尝试时卡在“VAD Sticker vad_dav.xml 创建”上
- wso2esb - 是否可以为 WSO2 ESB 中的排队请求设置最长生命周期?
- java - 在 apache ant 构建中替代 untar?
- amazon-web-services - AWS SES 批量发送问题