Reputation: 4255
I saw this question answering some of this, but at least not clearly my question.
I suspect that I should probably not access any global variables that requires code to execute (e.g. std::string
), but how about POD variables?
std::string s = "hello";
const char* c = "world";
extern std::string s2; // (actually below in the same TU)
__attribute__((constructor)) void init()
{
// safe to assume that !strcmp(c, "world");
// not safe to assume s == "hello"?
// even less safe to assume s2 == "foo";
}
std::string s2 = "foo";
Upvotes: 0
Views: 338
Reputation: 238351
The documentation says:
However, at present, the order in which constructors for C++ objects with static storage duration and functions decorated with attribute constructor are invoked is unspecified.
!strcmp(c, "world")
is probably safe to assume.
char* c = "world";
This is ill-formed because string literal doesn't implicitly convert to a pointer to non-const char (since C++11).
Upvotes: 3