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_;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With