Thanks for clearing that up a bit. So the scope of the object/
sub-struct would be the initialization block, and since that
doesn't make sense, anonymous recursive objects aren't allowed?
I don't think that's the issue. If I write something at
namespace scope like:
std::string s( str1 + str2 ) ;
where str1 and str2 are also strings, the operator+ returns a
temporary, which will be destructed once the initialization has
finished (supposing no RVO, which would merge the return value
and the object being constructed).
How's that behavior useful for anything? I'd expect the scope
to be that of the containing parent.
See above. The problem is that we're discussing something that
isn't currently present in C++, and trying to assimilate it to
features that are. For example, one way to get your code to
compile would be to provide constructors for the sub-object,
and---since you can't use the built-in operator & on a
temporary, a user defined operator & which returns this. The
problem is that as far as the compiler is concerned, this is
exactly like the case with string, above---the constructed
object et al. is just there to get the initial value, which will
end up in the actual declared object. And all of the
intermediate values will be destructed, and the memory for them
freed.
One possibility would be to give the hierarchial elements
constructors, then write something like:
st_b c =
{
{ 1, 2, new st_a( 0, 1 ) },
{ 3, 4, new st_a( 0, 2 ) },
// ...
} ;
Yes, it expects the type of the first union member. (Which I
find an unclear limitation; the whole idea of a union is to
hold any of its types. [I also see the {.union_member = 1}
syntax unnecessarily verbal]).
So how else would you choose. C90 says you can only initialize
the first element. C99 decided that this was overly
restrictive, and (for that and other reasons) introduced
designated initializers. What other syntax would you suggest?
In C++, you can initialize the element you want by using a
constructor.
But what's odd is that the first array element does accept a
non-first type, and only later array elements result in
compiler complaints.
You've confused me here. Could you give an example.
[...]
No, I was after initializing a hierarchial piece of data
concisely and cleanly.
Yes and no. You were trying to define several distinct objects
of different types in a single definition statement. That just
doesn't exist in C++; you can only define a single object with
static lifetime in each declarator. Any other objects created
by the initialization expression are temporaries, or if you use
the operator new, dynamically allocated.
The scope of the parent initialization is global, so I didn't
expect any freeing to be necessary.
It probably isn't. I don't delete my singletons either.
The C solution gives static initialization and static lifetime.
Using new in C++ gives dynamic initialization and dynamic
lifetime. If the objects have a POD type, you never delete
them, and they are created during initialization of static
objects, their effective lifetime is the same as if they were
static. But the initialization remains dynamic; if you use the
objects in the constructors of other static objects, you are
likely to run into order of construction problems.