Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best code for compiling static const int = X in VS2008 and GCC

I have run into a problem while writing C++ code that needs to compile in Visual Studio 2008 and in GCC 4.6 (and needs to also compile back to GCC 3.4): static const int class members.

Other questions have covered the rules required of static const int class members. In particular, the standard and GCC require that the variable have a definition in one and only one object file.

However, Visual Studio creates a LNK2005 error when compiling code (in Debug mode) that does include a definition in a .cpp file.

Some methods I am trying to decide between are:

  • Initialize it with a value in the .cpp file, not the header.
  • Use the preprocessor to remove the definition for MSVC.
  • Replace it with an enum.
  • Replace it with a macro.

The last two options are not appealing and I probably won't use either one. The first option is easy -- but I like having the value in the header.

What I am looking for in the answers is a good looking, best practice method to structure the code to make both GCC and MSVC happy at the same time. I am hoping for something wonderfully beautiful that I haven't thought of yet.

like image 278
Zan Lynx Avatar asked Aug 31 '11 21:08

Zan Lynx


1 Answers

I generally prefer the enum way, because that guarantees that it will always be used as immediate value and not get any storage. It is recognized as constant expression by the compiler.

class Whatever {
    enum { // ANONYMOUS!!!
        value = 42;
    };
    ...
}

If you can't go that way, #ifdef away the definition in the .cpp for MSVC, because if you ifdef away the value in declaration, it will always get storage; the compiler does not know the value, so it can't inline it (well, the "link time code generation" should be able to fix that up if enabled) and can't use it where constant is needed like value template arguments or array sizes.

like image 161
Jan Hudec Avatar answered Oct 03 '22 22:10

Jan Hudec