考虑以下代码。
#include <iostream>
class A {
public:
using T = float;
A(const T& x)
{
m_value = x;
}
T& value();
private:
T m_value;
};
// A::T& A::value()
// {
// return m_value;
// }
auto& A::value() -> T &
{
return m_value;
}
int main()
{
A a(10.0);
std::cout << a.value() << std::endl;
return 0;
}
使用 C++11 编译时,出现以下错误。
error: ‘value’ function with trailing return type has ‘auto&’ as its type rather than plain ‘auto’
auto& A::value()->T &
^
等效代码(注释函数)工作正常。但我想使用尾随返回类型。