Ho il seguente codice (semplificato), che compila bene in gcc, ma dà un errore in VS:VS2013 - static const già definito
// 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;
Errore:
main.obj : error LNK2005: "private: static int const Test::TEST" ([email protected]@@0HB) already defined in test.obj
Si tratta di un VS bug o gcc non mi permette in modo corretto di definire esplicitamente il membro const statico?
Aggiornamento: pensa che questa nel C++ Standard (9.4.2.3):
If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.20). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. — end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.
Aggiornamento # 2: trovato un bug report, che sostiene che si è risolto in prossima versione principale.
Cosa succede se si rimuove 'const int Test :: TEST;' dal proprio 'test.cpp', gcc compila il codice? – ixSci
Sembra funzionare, ma sto lavorando con il codice che definisce esplicitamente quelle linee e non voglio modificarlo poiché si interromperà nuovamente nel prossimo aggiornamento di svn. – riv
Grazie, volevo solo sapere se gcc ha un bug di tipo diverso. Pensavo che fosse necessario definire esplicitamente la costante in tutti i casi. Ma se così non fosse, c'è solo un bug in MSVC. – ixSci