Why I need to specify an anonymous union in a namespace scope as static?

Viewed 39

Why I can't define an Anonymous union in a namespace?

// in global namespace
union {
   int x_;
   int y_;
};

Or:

namespace Foo{
    union {
        int x_;
        int y_;
    };
}
  • Error: 'namespace-scope anonymous aggregates must be static'

  • But I solved it this way:

      namespace Foo{
          static union {
             int x_;
             int y_;
         };
     }
    

Or:

    void func(){
        union{
            int x_;
            int y_;
        }; / ok
    }

So can someone explain to me why I can only define an anonymous union in a namespace using the keyword static or I can define it as a local union?

0 Answers
Related