Message ID | 20181230160833.GU21364@redhat.com |
---|---|
State | New |
Headers | show |
Series | C++ PATCH for c++/88631, CTAD failing for value-initialization | expand |
On 12/30/18 11:08 AM, Marek Polacek wrote: > This PR points out that while we are able to deduce the template arguments > for A{}, we fail for A(). > > For A{}, the deduction happens in finish_compound_literal: > 2789 if (tree anode = type_uses_auto (type)) > 2790 if (CLASS_PLACEHOLDER_TEMPLATE (anode)) > 2791 { > 2792 type = do_auto_deduction (type, compound_literal, anode, complain, > 2793 adc_variable_type) > > and for A() in build_functional_cast, but there we always give error if there > are no parameters. That is wrong because even though there are no arguments > to deduce from, we might still be able to deduce from default template > arguments as in the test. Fixed thus; I'm passing tf_none if there are no > params because we don't want to give redundant diagnostics if the deduction > fails. OK. Jason
diff --git gcc/cp/typeck2.c gcc/cp/typeck2.c index cc9bf02439b..206a87b94a1 100644 --- gcc/cp/typeck2.c +++ gcc/cp/typeck2.c @@ -2147,9 +2147,18 @@ build_functional_cast (tree exp, tree parms, tsubst_flags_t complain) } else if (!parms) { - if (complain & tf_error) - error ("cannot deduce template arguments for %qT from ()", anode); - return error_mark_node; + /* Even if there are no parameters, we might be able to deduce from + default template arguments. Pass TF_NONE so that we don't + generate redundant diagnostics. */ + type = do_auto_deduction (type, parms, anode, tf_none, + adc_variable_type); + if (type == error_mark_node) + { + if (complain & tf_error) + error ("cannot deduce template arguments for %qT from ()", + anode); + return error_mark_node; + } } else type = do_auto_deduction (type, parms, anode, complain, diff --git gcc/testsuite/g++.dg/cpp1z/class-deduction59.C gcc/testsuite/g++.dg/cpp1z/class-deduction59.C new file mode 100644 index 00000000000..129d723d07c --- /dev/null +++ gcc/testsuite/g++.dg/cpp1z/class-deduction59.C @@ -0,0 +1,12 @@ +// PR c++/88631 +// { dg-do compile { target c++17 } } + +template<class T = void> +class A { }; + +int main() +{ + auto x = A(); + auto x2 = A{}; + A y; +}