On 8/7/24 10:40 AM, Jakub Jelinek wrote:
Hi!
The following patch attempts to implement DR2387 by making variable
templates including their specialization TREE_PUBLIC when at file
scope and they don't have static storage class.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
OK.
2024-08-07 Jakub Jelinek <ja...@redhat.com>
PR c++/109126
* decl.cc (grokvardecl): Implement CWG 2387 - Linkage of
const-qualified variable template. Set TREE_PUBLIC on variable
templates with const qualified types unless static is present.
* g++.dg/DRs/dr2387.C: New test.
* g++.dg/DRs/dr2387-aux.cc: New file.
--- gcc/cp/decl.cc.jj 2024-08-06 11:05:29.147469440 +0200
+++ gcc/cp/decl.cc 2024-08-07 11:58:11.275368835 +0200
@@ -11225,6 +11225,8 @@ grokvardecl (tree type,
|| ! constp
|| volatilep
|| inlinep
+ || in_template_context
+ || processing_specialization
|| module_attach_p ()));
TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
}
--- gcc/testsuite/g++.dg/DRs/dr2387.C.jj 2024-08-07 12:11:14.372115971
+0200
+++ gcc/testsuite/g++.dg/DRs/dr2387.C 2024-08-07 12:13:35.089273604 +0200
@@ -0,0 +1,22 @@
+// DR 2387
+// { dg-do run { target c++14 } }
+// { dg-additional-sources "dr2387-aux.cc" }
+
+template <int N>
+const int a = N;
+template <int N>
+static const int b = N;
+template <int N>
+extern const int c = N;
+template <int N>
+const volatile int d = N;
+template <int N>
+const int e = N;
+template <>
+const int e <43> = 44;
+
+const int *pa = &a <42>;
+const int *pb = &b <42>;
+const int *pc = &c <42>;
+const volatile int *pd = &d <42>;
+const int *pe = &e <43>;
--- gcc/testsuite/g++.dg/DRs/dr2387-aux.cc.jj 2024-08-07 12:11:09.388181223
+0200
+++ gcc/testsuite/g++.dg/DRs/dr2387-aux.cc 2024-08-07 12:13:25.321401491
+0200
@@ -0,0 +1,25 @@
+// DR 2387
+
+template <int N>
+extern const int a;
+template <int N>
+static const int b = N;
+template <int N>
+extern const int c;
+template <int N>
+extern const volatile int d;
+template <int N>
+extern const int e;
+extern const int *pa, *pb, *pc, *pe;
+extern const volatile int *pd;
+
+int
+main ()
+{
+ if (pa != &a <42>
+ || pb == &b <42>
+ || pc != &c <42>
+ || pd != &d <42>
+ || pe != &e <43>)
+ __builtin_abort ();
+}
Jakub