From 53de8a7e13cae771fdcda36abee4787bd6575ed7 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 23 Apr 2018 21:11:22 +0200 Subject: [PATCH] PR c++/85470 - wrong error with static data member. * decl.c (check_initializer): Check DECL_INITIALIZED_IN_CLASS_P. * typeck2.c (store_init_value): Likewise. Co-Authored-By: Jason Merrill From-SVN: r259571 --- gcc/cp/ChangeLog | 7 +++++++ gcc/cp/decl.c | 4 +++- gcc/cp/typeck2.c | 7 +++++-- gcc/testsuite/g++.dg/cpp0x/extern_template-4.C | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/extern_template-4.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 099407b..aaed5d4 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2018-04-23 Jakub Jelinek + Jason Merrill + + PR c++/85470 - wrong error with static data member. + * decl.c (check_initializer): Check DECL_INITIALIZED_IN_CLASS_P. + * typeck2.c (store_init_value): Likewise. + 2018-04-20 Jakub Jelinek PR c++/85462 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 9f1a171..d822745 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6513,7 +6513,9 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) } if (init_code - && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl))) + && (DECL_IN_AGGR_P (decl) + && DECL_INITIALIZED_IN_CLASS_P (decl) + && !DECL_VAR_DECLARED_INLINE_P (decl))) { static int explained = 0; diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index e5f9a68..37e7893 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -824,9 +824,12 @@ store_init_value (tree decl, tree init, vec** cleanups, int flags) bool const_init; value = fold_non_dependent_expr (value); if (DECL_DECLARED_CONSTEXPR_P (decl) - || (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl))) + || (DECL_IN_AGGR_P (decl) + && DECL_INITIALIZED_IN_CLASS_P (decl) + && !DECL_VAR_DECLARED_INLINE_P (decl))) { - /* Diagnose a non-constant initializer for constexpr. */ + /* Diagnose a non-constant initializer for constexpr variable or + non-inline in-class-initialized static data member. */ if (!require_constant_expression (value)) value = error_mark_node; else diff --git a/gcc/testsuite/g++.dg/cpp0x/extern_template-4.C b/gcc/testsuite/g++.dg/cpp0x/extern_template-4.C new file mode 100644 index 0000000..9f0c7d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/extern_template-4.C @@ -0,0 +1,23 @@ +// PR c++/85470 +// { dg-do compile { target c++11 } } + +template +struct StaticObject +{ + static T& create() + { + static T t; + return t; + } + + static T & instance; +}; + +template T & StaticObject::instance = StaticObject::create(); + +extern template class StaticObject; + +void test() +{ + StaticObject::instance; +} -- 2.7.4