返回类型扣除与私有成员变量 [英] Return type deduction with a private member variable
问题描述
正如此 Q& A昨天,g ++ 4.8和Clang 3.3都正确地抱怨下面的代码,如b_没有在此范围内声明
#include< iostream>
class Test
{
public:
Test():b_(0){}
auto foo()const - > ; decltype(b_)//只是省略 - > decltype(b_)使用c ++ 1y
{
return b_;
}
private:
int b_;
};
int main()
{
Test t;
std :: cout<< t.foo();
}
移动 private
部分到类定义的顶部消除了错误并输出0.
我的问题是,这个错误也会在C ++ 14中消失,返回类型扣除,以便我可以省略 decltype
并在我的 private
没有,但是没有了,我们已经开始使用这个工具了。msgctxt@info:whatsthis这是因为你可以说
decltype(auto)foo()const {
return b_;
}
这将从其正文自动推导返回类型。
As was explained in this Q&A yesterday, both g++ 4.8 and Clang 3.3 correctly complain about the code below with an error like "'b_' was not declared in this scope"
#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();
}
Moving the private
section to the top of the class definition eliminates the error and prints 0.
My question is, will this error also go away in C++14 with return type deduction, so that I can leave out the decltype
and have my private
section at the end of the class definition?
NOTE: It actually works based on @JesseGood 's answer.
No, but there not anymore is a need for this because you can say
decltype(auto) foo() const {
return b_;
}
This will deduce the return type automatically from its body.
这篇关于返回类型扣除与私有成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!