![](/img/trans.png)
[英]Friend function template with automatic return type deduction cannot access a private member
[英]Return type deduction with a private member variable
正如昨天的问答中所解释的那样 ,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();
}
将private
部分移动到类定义的顶部可消除错误并打印0。
我的问题是, 这个错误在C ++ 14中是否会随着返回类型推断而消失 ,因此我可以省略decltype
并在类定义的末尾使用我的private
部分?
注意 :它实际上是基于@JesseGood的答案。
不,但不再需要这个,因为你可以说
decltype(auto) foo() const {
return b_;
}
这将从其正文中自动推断出返回类型。
我不这么认为,因为C ++ 14会有自动返回类型扣除。 下面通过传递-std=c++1y
标志来编译g ++ 4.8。
auto foo() const
{
return b_;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.