In C++17 mode, build_special_member_call builds up an initializer for the object and then uses it directly rather than pass it to the copy constructor. This pattern results in a TARGET_EXPR showing up in potential_constant_expression, which complains about a TARGET_EXPR indicating a temporary of non-literal type, which it isn't in this case because it's being used to initialize a non-temporary object. This patch recognizes that case by checking TARGET_EXPR_DIRECT_INIT_P.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 9084980615c1aa29277fee9fe33a0b52bbc0ab94 Author: Jason Merrill <ja...@redhat.com> Date: Wed Jan 17 17:24:16 2018 -0500 PR c++/82461 - constexpr list-initialized member * constexpr.c (potential_constant_expression_1): Check TARGET_EXPR_DIRECT_INIT_P. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 8984613aa41..9a548d29bbc 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5726,7 +5726,8 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, return RECUR (TREE_OPERAND (t, 1), want_rval); case TARGET_EXPR: - if (!literal_type_p (TREE_TYPE (t))) + if (!TARGET_EXPR_DIRECT_INIT_P (t) + && !literal_type_p (TREE_TYPE (t))) { if (flags & tf_error) { diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-list2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-list2.C new file mode 100644 index 00000000000..780a64dbbc4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-list2.C @@ -0,0 +1,20 @@ +// PR c++/82461 +// { dg-do compile { target c++11 } } + +class A { +private: +public: + constexpr A() {} + ~A() {} +}; + +class B { +private: + A a; +public: + constexpr B() : a{} {} +// works +// constexpr B() : a() {} + + ~B() {} +};