开发者

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

开发者 https://www.devze.com 2023-04-01 19:47 出处:网络
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.

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.


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.


If you don't dislike the idea of using non-standard hacks, for VC++ there's always __declspec(selectany). My understanding is that it will ensure that at link time, any conflicts are resolved by dropping all but one definition. You can potentially put this in an #ifdef _MSC_VER block.


The Visual C++ 2010 accepts this:

// test.hpp:
struct test {
    static const int value;
};

// test.cpp:
#include "test.hpp"
const int test::value = 10;


This is still a problem with VS2013. I've worked around it by putting my standard-compliant definition, in the cpp file, inside a #if preventing VS.

a.h:

class A
{
public:
  static unsigned const a = 10;
};

a.cpp:

#ifndef _MSC_VER
unsigned const A::a;
#endif

I also commented it well, so the next guy in the file knows which compiler to blame.

0

精彩评论

暂无评论...
验证码 换一张
取 消