diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index cdd7701b9e7..ee290fc03c4 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -4128,6 +4128,14 @@ add_list_candidates (tree fns, tree first_arg, if (CONSTRUCTOR_NELTS (init_list) == 0 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)) ; + else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list) + && !CP_AGGREGATE_TYPE_P (totype)) + { + if (complain & tf_error) + error ("designated initializers cannot be used with a " + "non-aggregate type %qT", totype); + return; + } /* If the class has a list ctor, try passing the list as a single argument first, but only consider list ctors. */ else if (TYPE_HAS_LIST_CTOR (totype)) @@ -4139,14 +4147,6 @@ add_list_candidates (tree fns, tree first_arg, if (any_strictly_viable (*candidates)) return; } - else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list) - && !CP_AGGREGATE_TYPE_P (totype)) - { - if (complain & tf_error) - error ("designated initializers cannot be used with a " - "non-aggregate type %qT", totype); - return; - } /* Expand the CONSTRUCTOR into a new argument vec. */ vec *new_args; diff --git a/gcc/testsuite/g++.dg/cpp2a/desig27.C b/gcc/testsuite/g++.dg/cpp2a/desig27.C new file mode 100644 index 00000000000..8d4cf483176 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig27.C @@ -0,0 +1,16 @@ +// PR c++/109871 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +#include + +struct vector { + vector(std::initializer_list); // #1 + vector(int); // #2 +}; + +void f(vector); + +int main() { + f({.blah = 42}); // { dg-error "designated" } previously incorrectly selected #2 +}