This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for c++/35075
- From: Jason Merrill <jason at redhat dot com>
- To: gcc-patches List <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 13 Nov 2009 09:39:20 -0500
- Subject: C++ PATCH for c++/35075
Just needed to handle the case where the argument isn't a decl properly.
And I figured that we might as well give a better error to help people
understand why a reference variable isn't a valid template argument for
a reference parameter.
Tested x86_64-pc-linux-gnu, applied to trunk.
commit ab62e510459364e94f1764d96ac794e88a5afcf4
Author: Jason Merrill <jason@redhat.com>
Date: Fri Nov 13 08:14:32 2009 -0500
PR c++/35075
* pt.c (convert_nontype_argument): Give helpful error about
reference variable argument to reference template parameter.
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 85d9fff..c799368 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -4927,6 +4927,27 @@ convert_nontype_argument (tree type, tree expr)
shall be one of: [...]
-- the address of an object or function with external linkage. */
+ if (TREE_CODE (expr) == INDIRECT_REF
+ && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
+ {
+ expr = TREE_OPERAND (expr, 0);
+ if (DECL_P (expr))
+ {
+ error ("%q#D is not a valid template argument for type %qT "
+ "because a reference variable does not have a constant "
+ "address", expr, type);
+ return NULL_TREE;
+ }
+ }
+
+ if (!DECL_P (expr))
+ {
+ error ("%qE is not a valid template argument for type %qT "
+ "because it is not an object with external linkage",
+ expr, type);
+ return NULL_TREE;
+ }
+
if (!DECL_EXTERNAL_LINKAGE_P (expr))
{
error ("%qE is not a valid template argument for type %qT "
diff --git a/gcc/testsuite/g++.dg/template/ref5.C b/gcc/testsuite/g++.dg/template/ref5.C
new file mode 100644
index 0000000..efefbef
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ref5.C
@@ -0,0 +1,11 @@
+// PR c++/35075
+
+template<int&> struct A {};
+
+template<typename T> struct B
+{
+ static const T t;
+ A<t> a; // { dg-error "reference variable" }
+};
+
+B<int&> b; // { dg-message "instantiated" }