Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 907
user5024425 Avatar asked Feb 19 '26 05:02

user5024425


1 Answers

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.

like image 175
uh oh somebody needs a pupper Avatar answered Feb 21 '26 19:02

uh oh somebody needs a pupper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!