type = cp_parser_new_type_id (parser, &nelts);
/* If the next token is a `(' or '{', then we have a new-initializer. */
- if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
- || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
+ cp_token *token = cp_lexer_peek_token (parser->lexer);
+ if (token->type == CPP_OPEN_PAREN
+ || token->type == CPP_OPEN_BRACE)
initializer = cp_parser_new_initializer (parser);
else
initializer = NULL;
expression. */
if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
ret = error_mark_node;
+ /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
+ of a new-type-id or type-id of a new-expression, the new-expression shall
+ contain a new-initializer of the form ( assignment-expression )".
+ Additionally, consistently with the spirit of DR 1467, we want to accept
+ 'new auto { 2 }' too. */
+ else if (type_uses_auto (type)
+ && (vec_safe_length (initializer) != 1
+ || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
+ && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
+ {
+ error_at (token->location,
+ "initialization of new-expression for type %<auto%> "
+ "requires exactly one element");
+ ret = error_mark_node;
+ }
else
{
/* Create a representation of the new-expression. */
--- /dev/null
+// PR c++/51911
+// { dg-do compile { target c++11 } }
+
+#include <initializer_list>
+
+auto foo1 = new auto { 3, 4, 5 }; // { dg-error "22:initialization of new-expression for type 'auto'" }
+auto bar1 = new auto { 2 };
+
+auto foo2 = new auto ( 3, 4, 5 ); // { dg-error "22:initialization of new-expression for type 'auto'" }
+auto bar2 = new auto ( 2 );