相关疑难解决方法(0)

用户定义的转换运算符模板和内置运算符:不匹配运算符

考虑以下MCVE.

#include <type_traits>

struct A {
    template<typename T, typename std::enable_if<std::is_same<T,int>::value,int>::type = 0>
    operator T() const { return static_cast<T>(1); }
};

int main() {
    int x = 1;
    A a;
    return x + a;
}
Run Code Online (Sandbox Code Playgroud)

clang汇编得很好.DEMO

海湾合作委员会失败了:

error: no match for 'operator+' (operand types are 'int' and 'A')
  return x + a;
         ~~^~~
Run Code Online (Sandbox Code Playgroud)

问题:谁是对的?为什么?

c++ language-lawyer c++11

9
推荐指数
1
解决办法
167
查看次数

标签 统计

c++ ×1

c++11 ×1

language-lawyer ×1