]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: recognize in-class var tmpl partial spec [PR71954]
authorPatrick Palka <ppalka@redhat.com>
Fri, 11 Aug 2023 17:25:58 +0000 (13:25 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 11 Aug 2023 17:25:58 +0000 (13:25 -0400)
This makes us recognize member variable template partial specializations
defined directly inside the class body.  It seems we mainly just need to
call check_explicit_specialization when we see a static TEMPLATE_ID_EXPR
data member, which sets SET_DECL_TEMPLATE_SPECIALIZATION for us and which
we otherwise don't call (for the out-of-class case we call it from
grokvardecl).

We also need to make finish_member_template_decl return NULL_TREE for
such partial specializations, matching its behavior for class template
partial specializations, so that later we don't try to register it as a
separate member declaration.

PR c++/71954

gcc/cp/ChangeLog:

* decl.cc (grokdeclarator): Pass 'dname' instead of
'unqualified_id' as the name when building the VAR_DECL for a
static data member.  Call check_explicit_specialization for a
TEMPLATE_ID_EXPR such member.
* pt.cc (finish_member_template_decl): Return NULL_TREE
instead of 'decl' when DECL_TEMPLATE_SPECIALIZATION is not
set.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/var-templ84.C: New test.
* g++.dg/cpp1y/var-templ84a.C: New test.

gcc/cp/decl.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1y/var-templ84.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/var-templ84a.C [new file with mode: 0644]

index 792ab330dd056e90cd4df1b59386045ded988430..e21e015d5d8f2985c707d97179af91c3cf243fee 100644 (file)
@@ -14439,7 +14439,16 @@ grokdeclarator (const cp_declarator *declarator,
                /* C++ allows static class members.  All other work
                   for this is done by grokfield.  */
                decl = build_lang_decl_loc (id_loc, VAR_DECL,
-                                           unqualified_id, type);
+                                           dname, type);
+               if (unqualified_id
+                   && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
+                 {
+                   decl = check_explicit_specialization (unqualified_id, decl,
+                                                         template_count,
+                                                         concept_p * 8);
+                   if (decl == error_mark_node)
+                     return error_mark_node;
+                 }
                set_linkage_for_static_data_member (decl);
                if (concept_p)
                  error_at (declspecs->locations[ds_concept],
index 2706fa619bf8b8a4e9fd01539c60221a4e725d23..a4809f034dcefec585cb72b9a8181ce97fb5d18b 100644 (file)
@@ -314,7 +314,7 @@ finish_member_template_decl (tree decl)
          return DECL_TI_TEMPLATE (decl);
        }
       else
-       return decl;
+       return NULL_TREE;
     }
   else
     error_at (DECL_SOURCE_LOCATION (decl),
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C
new file mode 100644 (file)
index 0000000..39c2a0d
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/71954
+// { dg-do compile { target c++14 } }
+
+struct A {
+  template<class T> static const int var = 0;
+  template<class T> static const int var<T*> = 1;
+  template<class T> static const int var<const T*> = 2;
+};
+
+static_assert(A::var<int> == 0, "");
+static_assert(A::var<int*> == 1, "");
+static_assert(A::var<const int*> == 2, "");
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C
new file mode 100644 (file)
index 0000000..4aa3a2a
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/71954
+// A version of var-templ84.C where the partial specializations depend on
+// outer template parameters.
+// { dg-do compile { target c++14 } }
+
+template<class T>
+struct A {
+  template<class U, class V> static const int var = 0;
+  template<class U> static const int var<U*, T> = 1;
+  template<class U> static const int var<const U*, T> = 2;
+};
+
+static_assert(A<int>::var<int, int> == 0, "");
+static_assert(A<int>::var<int*, int> == 1, "");
+static_assert(A<int>::var<const int*, int> == 2, "");
+
+static_assert(A<int>::var<int, char> == 0, "");
+static_assert(A<int>::var<int*, char> == 0, "");
+static_assert(A<int>::var<const int*, char> == 0, "");