This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix PR c++/30759: Initializer-list accepted for object of non-POD type
- From: Simon Martin <simartin at users dot sourceforge dot net>
- To: Gabriel Dos Reis <gdr at cs dot tamu dot edu>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Mon, 28 May 2007 21:43:39 +0200
- Subject: Re: [PATCH] Fix PR c++/30759: Initializer-list accepted for object of non-POD type
- References: <46588025.20605@users.sourceforge.net> <87irafh10j.fsf@soliton.cs.tamu.edu>
Hello.
Gabriel Dos Reis wrote:
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.
Thanks for the information, I did not know about this.
I attach an updated patch that will not issue this error when passing
"-std=c++0x" or "std=gnu++0x" (the test case and associated ChangeLog
entry are unchanged).
I've successfully regtested it on i386-apple-darwin8.9.1. Is it OK?
Best regards,
Simon
2007-05-26 Simon Martin <simartin@users.sourceforge.net>
PR c++/30759
* decl.c (check_initializer): Report an error when a brace enclosed
initializer is used for a non-aggregate type except in C++0x.
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c (revision 125113)
+++ gcc/cp/decl.c (working copy)
@@ -4777,14 +4777,29 @@ check_initializer (tree decl, tree init,
else
/* There is no way to make a variable-sized class type in GNU C++. */
gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
-
- if (!CP_AGGREGATE_TYPE_P (type)
- && init && BRACE_ENCLOSED_INITIALIZER_P (init)
- && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1)
- {
- error ("scalar object %qD requires one element in initializer", decl);
- TREE_TYPE (decl) = error_mark_node;
- return NULL_TREE;
+
+ if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
+ {
+ int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
+ if (SCALAR_TYPE_P (type))
+ {
+ if (init_len != 1)
+ {
+ error ("scalar object %qD requires one element in initializer",
+ decl);
+ TREE_TYPE (decl) = error_mark_node;
+ return NULL_TREE;
+ }
+ }
+ else if (!flag_cpp0x && !CP_AGGREGATE_TYPE_P (type))
+ {
+ /* A non-aggregate that is not a scalar cannot be initialized
+ via an initializer-list except in C++0x. */
+ error ("braces around initializer for non-aggregate type %qT",
+ type);
+ TREE_TYPE (decl) = error_mark_node;
+ return NULL_TREE;
+ }
}
if (TREE_CODE (decl) == CONST_DECL)