I have the following code (simplified) that compiles in gcc but gives an error in VS:
// main.cpp #include "test.h" int main() { return 0; } // test.h #pragma once class Test { static const int TEST = 3; }; // test.cpp #include "test.h" const int Test::TEST;
Mistake:
main.obj : error LNK2005: "private: static int const Test::TEST" ( ?TEST@Test @@0HB) already defined in test.obj
Is this a VS error or is gcc incorrectly allowing me to explicitly define a static const member?
Update: found this in C ++ Standard (9.4.2.3):
If a constant statistic element with a constant stabilizer is integral or enumerable, its declaration in the definition class may indicate a bit-or-equal-initializer, in which each initializer clause, which is an assignment expression, is a constant expression (5.20). A static literal data member can be declared in a class definition using the constexpr specifier; if so, his declaration specifies a logical or equal-initializer in which each initializing clause, which is an assignment expression, is a constant expression. [Note: in both of these cases, a member may appear in constant expressions. - end note] A member must be defined in the namespace area if used in odr (3.2) in the program, and the namespace area definition must not contain an initializer.
Update # 2: an error report was found stating that it was fixed in the next major version.
c ++ visual-studio visual-studio-2013
riv
source share