0

Consider the following code:

class Main
{
  private:
      class Inner
      {
        public:
          void DoSomething() {}
      };

  public:
    static Inner* CreateInner() { return new Inner(); }
 };

It is impossible to compile the following:

Main::Inner* pInner = Main::CreateInner();
pInner->DoSomething();

Because we'll get a compiler error 'C2248: cannot access private class decalred in class Main'

However, using the following syntax will compile and execute smoothly:

auto pInner = Main::CreateInner();
pInner->DoSomething();

Suddenly, (to my surprise, anyway...), the inner private class is 'visible' to the outside world...

Does anyone aware to this compiler behavior? Is it considered 'valid' to use the auto keyword according to the C++11 standard in this case, or is it some kind of a bug?

It behaves as described on both:

  1. Visual Studio 2010 - platform toolset v100
  2. Visual Studio 2013 - platform toolset v120

Any thoughts?

Thanks!

sepp2k
  • 363,768
  • 54
  • 674
  • 675
AlphaP8
  • 17
  • 2
  • I would be quite surprised if you got the second case to compile in VS2010 as [`auto`](http://en.cppreference.com/w/cpp/language/auto) was introduced in C++11 which is (mostly sorta) supported starting as of VS2013. – Cory Kramer Dec 30 '15 at 13:57
  • Confirmed behaviour with gcc 5.2 http://coliru.stacked-crooked.com/a/db691f65c9c806d0 – YSC Dec 30 '15 at 13:58
  • I confirmed this behaviour with C++14 compiler. Here is code link, https://ideone.com/ef7HMO (call to DoSomething() is successful) – Pravar Jawalekar Dec 30 '15 at 14:00
  • This link explains a few things about this behavior http://stackoverflow.com/questions/13532784/why-can-i-use-auto-on-a-private-type – Pravar Jawalekar Dec 30 '15 at 14:02
  • It does compile in Visual Studio 2010, because, although it is prior to C++11, VS2010 included the 'auto' keyword along with other C++0x features (as it was called prior to C++11) . See here: https://visualstudiomagazine.com/Articles/2010/04/01/The-Evolution-of-Visual-C-in-Visual-Studio-2010.aspx – AlphaP8 Dec 30 '15 at 14:28

0 Answers0