[PATCH] Fix PR c++/30759: Initializer-list accepted for object of non-POD type

Gabriel Dos Reis gdr@cs.tamu.edu
Sat May 26 19:00:00 GMT 2007


Simon Martin <simartin@users.sourceforge.net> writes:

| Hi all.
| 
| The following invalid snippet is accepted since version 3.4:
| 
| === cut here ===
| struct A {
|      A(int) { }
| };
| int main()
| {
|      A a = { 6 };
| }
| === cut here ===
| 
| It is invalid because A is not an aggregate (due to its constructor),
| and its instances cannot therefore be initialized via an initializer list.
| 
| The problem is that the only check that we do in 'check_initializer'
| when dealing with an initializer list for a non-aggregate is that there
| is only one element in that list.
| 
| If I read the standard correctly, this constraint is for scalars, not
| any non-aggregate (the error message that is issued if it is broken
| suggests this as well: "scalar object %qD requires one element in
| initializer").
| 
| The attached patch modifies 'check_initializer' so that when dealing
| with an initializer list:
|    - For a scalar, we ensure that the initializer list only contains one
| element
|    - For a non-aggregate type that is not a scalar (e.g. 'A' in the
| snippet above), we issue the following error: "braces around initializer
| for non-aggregate type %qT".
| 
| I have regtested it on i386-apple-darwin8.9.1 with no new unexpected
| failures. Is it OK for mainline?

This is an area where C++0x will change.  I would suggest that you
check for C++98 and C++03 for the code path that rejects this code.

-- Gaby



More information about the Gcc-patches mailing list