Tem*_*Rex 18 c++ decltype c++11 return-type-deduction c++14
正如昨天的问答中所解释的那样,g ++ 4.8和Clang 3.3都正确地抱怨下面的代码,并且在这个范围内未声明"'b_'未声明"
#include <iostream>
class Test
{
public:
Test(): b_(0) {}
auto foo() const -> decltype(b_) // just leave out the -> decltype(b_) works with c++1y
{
return b_;
}
private:
int b_;
};
int main()
{
Test t;
std::cout << t.foo();
}
Run Code Online (Sandbox Code Playgroud)
将该private部分移动到类定义的顶部可消除错误并打印0.
我的问题是,这个错误在C++ 14中是否会随着返回类型推断而消失,这样我就可以省略decltype并private在类定义的末尾使用我的部分?
注意:它实际上是基于@JesseGood的答案.
Joh*_*itb 22
不,但不再需要这个,因为你可以说
decltype(auto) foo() const {
return b_;
}
Run Code Online (Sandbox Code Playgroud)
这将从其正文中自动推断出返回类型.
我不这么认为,因为C++ 14会有自动返回类型扣除.以下通过传递-std=c++1y标志来编译g ++ 4.8 .
auto foo() const
{
return b_;
}
Run Code Online (Sandbox Code Playgroud)