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.
I don't think so, because C++14 will have automatic return type deduction. The following compiles with g++ 4.8 by passing the -std=c++1y
flag.
auto foo() const
{
return b_;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.