[Bug c++/88572] error: braces around scalar initializer - should be a warning

wjwray at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Jan 7 22:16:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88572

--- Comment #9 from Will Wray <wjwray at gmail dot com> ---
The patch below seems to work as far as I've tested - please review.

It looks like the bool first_initializer_p argument to reshape_init_r
gives the context that is needed, according to the function comment;

/* ...
   FIRST_INITIALIZER_P is true if this is the first initializer of the
   outermost CONSTRUCTOR node.  */

If I read this right then this bool arg is true for scalar init and
false when initializing a scalar member of an aggregate (class or array).

The patch below rejects
int ii{{0}};

--- gcc/cp/decl.c
+++ gcc/cp/decl.c
@@ -6054,16 +6054,19 @@ reshape_init_r (tree type, reshape_iter
        {
          if (SCALAR_TYPE_P (type))
            {
-             if (cxx_dialect < cxx11
-                 /* Isn't value-initialization.  */
-                 || CONSTRUCTOR_NELTS (stripped_init) > 0)
+             if (cxx_dialect < cxx11 || first_initializer_p)
                {
                  if (complain & tf_error)
                    error ("braces around scalar initializer for type %qT",
                           type);
                  init = error_mark_node;
                }
-           }
+              else if (CONSTRUCTOR_NELTS (stripped_init) > 0)
+               {
+                 warning (0, "braces around scalar initializer for type %qT",
+                          type);
+               }
+            }
          else
            maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
        }


More information about the Gcc-bugs mailing list