(I was asked this question recently.)
I want to use the C++ static_block
construct to initialize static
field of a class:
static_block {
myns::foo my_foo;
auto s = my_foo.from_string("null");
if (s.good()) {
std::string bar::transmogrified_foo = my_foo.transmogrify();
} else {
std::string bar::transmogrified_foo = "";
}
}
where transmogrified_foo
is declared in the class as:
static std::string transmogrified_foo;
However, I get the following error:
error: definition or redeclaration of 'transmogrified_foo' not allowed inside a function
Do you have suggestion how the static field should be initialized ?