首页 > 解决方案 > 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.

标签: c++g++clang++

解决方案


这看起来像一个clang错误。

不合格的名称查找

对于在类定义(包括基类说明符和嵌套类定义)中使用的名称,除了在成员函数体内部、成员函数的默认参数、成员函数的异常规范或默认成员初始值设定项,其中成员可以属于一个嵌套类,其定义在封闭类的主体中,搜索以下范围:

a) 在使用点之前使用名称的类的主体

...

根据上述,您的用户定义的转换函数必须在尾随返回类型中可见。

这是成员函数的另一个示例,它在 gcc 上运行良好,但在 clang 上无法编译。

void f(A a, decltype(int(A)) {}

推荐阅读