I'm trying to use compounds the {} notation of initializer lists for initializing static local constants in C++11.
The question might be a follow-up of one about temporary arrays.
There may be a side track(?) hinting at const (but only for C99?).
Another question brings heap allocation into view, but with a difference,
I think.
The idea is for a function to establish a data structure in its own
scope, once. A static
(const) variable definition takes values from the lists as written (by misguided intention: C-style
compound literals). Later, the function should be able to refer to the
structure. The SO links above explain, both directly and indirectly,
why just literals won't work.
Will new
help, though?
int undef()
{
static const int vs[3] {1,2,3};
static const int* vss[2] {
vs,
// (const int[3]){1,2,3} // temporary will be lost
new const int[3] {1,2,3}
};
return vss[0][1] + vss[1][1];
}
My guess: new
will allocate an array somewhere, initialize the
new array from the temporary {1,2,3}
and then that somewhere continues to have an
address. So, nothing is lost. Correct?
Is the above use of new
safe and sound, meaning covered by the
standard? C++11 in my case.