Mateusz Loskot :: hacking on, working out, living up

Yet another trap in the eVC++ compiler

24 Jan 2006 | mloskot

Today, I was porting a fairly big library to use it on Windows CE. I created a library project with eVC++ 4.0 then added source files of the library and with pretty smile on my face I hit F7. Then I was pelted with huge amount of error messages :-( Shortly, I revealed where was the problem. I thought it will bowl over everything I planned! I took a deep breath and… Simply, eVC++ 4.0 compiler, as well as VC++ 6.0 (and earlier), does not support in-place initialization of static const member data of class!

So, following code can not be compiled with eVC++ 4.0: class A { public: static const int counter = 123; }; There are two workarounds: use enum defined inside your class or instantiate static const member outside the class. The later is my prefered solution: class A { public: static const int counter; // = 123; }; const int A::counter = 123; IMHO, this version is more consistent with how non-const static data members of class are initialized - outside the class.

After I realized where is the problem and did backport changes in about 60 lines of ported library code I found that it’s known issue and it’s already reported in KB as Error C2258 and error C2252 occur if you try to perform in-place initialization of static const integral member data in Visual C++. So, be watchful when playing with modern C++ code on old compiler :-)

Fork me on GitHub