const struct member in aggregate definition

The other day I updated the contents of a cpp file containing assorted static data definitions to tighten up the const-ness of the assorted string; from const char* to char const * const. In doing so, In advertently converted a couple of structure definitions.

Now, I thought that the following kind of thing

1
2
3
4
5
6
7
8
struct Test
{
    char const * const name;
};

const Test aTest[] = {
    {"One", "Two", "Three", "Testing"} 
};


was not allowed!?

But both gcc and VC compile it, though VC does complain that it can't generate the required constructor and assignment operator.

My understanding was that:
1 - only POD struts could be used in aggregates (For those who haven't yet encountered the term: POD = "plain old data" = C struct, with no constructors, base classes, virtual methods)
2 - if a class/struct has a const or ref data member, these must be unitialized in the intializer list, which means there must be a constructor. Hence it is not POD.

So the constness here should mean that the struct is no longer a POD, and hence the aggregate is invalid.

Or is there another rule that come into play to allow this?

Andy
Last edited on
C++ Standard section 8.5.1 wrote:
An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).


If a const member would need to be initialized in a constructor, that doesn't mean there is one.
Topic archived. No new replies allowed.