简体   繁体   中英

Initializing static data member from non-static member

I'm trying to initialize a static member variable from a sizeof of a non-static:

class Myclass
{
   int a;
   static const int b = sizeof(a);
};

This should be legal with c++11, right? It gives me compiler error C2327 ("a is no typename not static and no enumerator).

Compiler is Visual Studio 2015 Express.

This is obviously a bug in VC++, as in 19.00.23506, sizeof(Myclass::a) doesn't work. This seems to have been fixed somewhere between versions 19.00.23506 (try online rextester ) and 19.00.23720.0 (try online Microsoft ). You may try to report the bug or download a newer build of the IDE/compiler.

Still, I would report a bug anyways since the nested name specifier shouldn't be required.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM