If I write code that looks like
#include <string>
class C {
static const std::string MY_SPECIAL_STRING = "hi";
};
and I try to compile it, even in C++11 mode, g++ will complain:
static data member of type 'const std::string' must be initialized out of line
Why do we have this rule?
I mean, I can still kind of 'cheat' and have static variables inside static methods, e.g.:
#include <string>
class C {
static const std::string& MY_SPECIAL_STRING() {
static const std::string& ss("hi");
return ss;
}
};
This way, I don't need to put a declaration in a translation unit, and I effectively have the same thing, albeit with uglier syntax.
Aucun commentaire:
Enregistrer un commentaire