Since r8-509, we'll no longer create a static temporary var for the initializer '{ 1, 2 }' for num in the attached test because the code in finish_compound_literal is now guarded by '&& fcl_context == fcl_c99' but it's fcl_functional here. This causes us to reject num as non-constant when evaluating it in a template.
Jason's idea was to treat num as value-dependent even though it actually isn't. This patch implements that suggestion. The is_really_empty_class check is sort of non-obvious but the comment should explain why I added it. Co-authored-by: Jason Merrill <ja...@redhat.com> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/109876 gcc/cp/ChangeLog: * pt.cc (value_dependent_expression_p) <case VAR_DECL>: Treat a constexpr-declared non-constant variable as value-dependent. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-template12.C: New test. * g++.dg/cpp1z/constexpr-template1.C: New test. --- gcc/cp/pt.cc | 12 ++++++ .../g++.dg/cpp0x/constexpr-template12.C | 38 +++++++++++++++++++ .../g++.dg/cpp1z/constexpr-template1.C | 25 ++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 7fb3e75bceb..38fd8070705 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -27969,6 +27969,18 @@ value_dependent_expression_p (tree expression) else if (TYPE_REF_P (TREE_TYPE (expression))) /* FIXME cp_finish_decl doesn't fold reference initializers. */ return true; + /* We have a constexpr variable and we're processing a template. When + there's lifetime extension involved (for which finish_compound_literal + used to create a temporary), we'll not be able to evaluate the + variable until instantiating, so pretend it's value-dependent. */ + else if (DECL_DECLARED_CONSTEXPR_P (expression) + && !TREE_CONSTANT (expression) + /* When there's nothing to initialize, we'll never mark the + VAR_DECL TREE_CONSTANT, therefore it would remain + value-dependent and we wouldn't instantiate. */ + && !is_really_empty_class (TREE_TYPE (expression), + /*ignore_vptr*/false)) + return true; return false; case DYNAMIC_CAST_EXPR: diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C new file mode 100644 index 00000000000..a9e065320c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C @@ -0,0 +1,38 @@ +// PR c++/109876 +// { dg-do compile { target c++11 } } + +using size_t = decltype(sizeof 0); + +namespace std { +template <class> struct initializer_list { + const int *_M_array; + size_t _M_len; + constexpr size_t size() const { return _M_len; } +}; +} // namespace std + +constexpr std::initializer_list<int> gnum{2}; + +template <int> struct Array {}; +template <int> void g() +{ + static constexpr std::initializer_list<int> num{2}; + static_assert(num.size(), ""); + Array<num.size()> ctx; + + constexpr Array<1> num1{}; +} + +template <int N> +struct Foo +{ + static constexpr std::initializer_list<int> num = { 1, 2 }; + static_assert(num.size(), ""); + Array<num.size()> ctx; +}; + +void +f (Foo<5>) +{ + g<0>(); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C new file mode 100644 index 00000000000..58be046fd36 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C @@ -0,0 +1,25 @@ +// PR c++/109876 +// { dg-do compile { target c++17 } } + +struct Foo {}; +template <const Foo&> struct X {}; + +void g() +{ + static constexpr Foo foo; + X<foo> x; +} + +template<int> +void f() +{ + static constexpr Foo foo; + X<foo> x; +} + +void +h () +{ + f<0>(); + f<1>(); +} base-commit: 8d6bd830f5f9c939e8565c0341a0c6c588834484 -- 2.40.1