PR c++/85470 - wrong error with static data member.
authorJakub Jelinek <jakub@redhat.com>
Mon, 23 Apr 2018 19:11:22 +0000 (21:11 +0200)
committerJason Merrill <jason@gcc.gnu.org>
Mon, 23 Apr 2018 19:11:22 +0000 (15:11 -0400)
* decl.c (check_initializer): Check DECL_INITIALIZED_IN_CLASS_P.
* typeck2.c (store_init_value): Likewise.

Co-Authored-By: Jason Merrill <jason@redhat.com>
From-SVN: r259571

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/cp/typeck2.c
gcc/testsuite/g++.dg/cpp0x/extern_template-4.C [new file with mode: 0644]

index 099407b9a14302c9c851bbfae5bb7b6341787b1c..aaed5d4d01bb927cdb837067bcaae1cd6f42c379 100644 (file)
@@ -1,3 +1,10 @@
+2018-04-23  Jakub Jelinek  <jakub@redhat.com>
+           Jason Merrill  <jason@redhat.com>
+
+       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  <jakub@redhat.com>
 
        PR c++/85462
index 9f1a171ead79066e281e82c1e33c971aa7589bc5..d822745b81f19bf0f6a0e7675fb0010770b03f24 100644 (file)
@@ -6513,7 +6513,9 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **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;
 
index e5f9a68ec583354ba4a28fd60df51e683d020210..37e7893610332d8a2231b2ca82a24c2b7a82a28d 100644 (file)
@@ -824,9 +824,12 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** 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 (file)
index 0000000..9f0c7d7
--- /dev/null
@@ -0,0 +1,23 @@
+// PR c++/85470
+// { dg-do compile { target c++11 } }
+
+template <class T>
+struct StaticObject
+{
+    static T& create()
+    {
+      static T t;
+      return t;
+    }
+
+    static T & instance;
+};
+
+template <class T> T & StaticObject<T>::instance = StaticObject<T>::create();
+
+extern template class StaticObject<int>;
+
+void test()
+{
+    StaticObject<int>::instance;
+}