在模板中返回一个没有返回类型声明的值,这是一个错字吗?

All*_*nzi 6 c++ template-meta-programming c++11

我正在看Walter E. Brown的现代模板元编程.在54:40,给出如下代码

template<class T, T v>
struct integral_constant{
  static constexpr T value = v;
   constexpr  operator T() const noexcept { return value; } // what does this mean?
   constexpr T operator T() const noexcept { return value; }
};
Run Code Online (Sandbox Code Playgroud)

我的问题是这条线是什么意思 constexpr operator T() const noexcept { return value; },为什么没有返回类型但它仍在返回value?这是拼写错误吗?

dev*_*fan 9

是的,第二个操作员行是错误的,可以完全删除.

类型操作符,例如.operator int()被执行
时,对象被浇铸或隐式转换为类型:

MyClass myObject;
int i = myObject; // here operator int() is used.
Run Code Online (Sandbox Code Playgroud)

当然,operator int()必须回归int.没有必要或允许为这些运算符编写特定的返回类型.在你的情况,it's没有intfloat或特定的东西,但模板类型,但it's同样的想法.

除了返回类型问题,第二个操作符行再次定义具有相同参数的相同操作符,不能有多个具有相同名称和参数的函数.

在整体之后struct,分号丢失了.

解决这些问题之后,它会编译:http://ideone.com/Hvrex5