0

Does anybody know the reason why the line with auto & compiles fine? I tried this with clang 3.6 (C++17), GCC 5.2 (C++17), VS2013 (update5) and they all allow this. I know this example does not make much sense, but still why the difference via auto ?

When I use Scott Meyers trick with the TD (type displayer), it shows me that the real type of the auto &b is A::B as well. So again, why the difference?

#include <iostream>

using namespace std;

class A
{
private:
   class B
      {
      public:
         void x()
            {
            std::cout << "inside B::x()" << std::endl;
            }
      };

public:   
   B& getB() { return m_memberB; }

private:
   B m_memberB;
};

int main()
{
A a1;

//A::B& b = a1.getB();  // this doesn't compile  (A::B is private...)
auto &b = a1.getB();    // this compiles!
b.x();

return 0;
}
tangens
  • 129
  • 6

0 Answers0