This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Patch for constexpr variable templates
- From: Andrew Sutton <andrew dot n dot sutton at gmail dot com>
- To: Jason Merrill <jason at redhat dot com>
- Cc: Paolo Carlini <paolo dot carlini at oracle dot com>, Braden Obrzut <admin at maniacsvault dot net>, Adam Butcher <adam at jessamine dot co dot uk>, Ed Smith-Rowland <3dw4rd at verizon dot net>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 7 Aug 2014 09:22:57 -0400
- Subject: Re: Patch for constexpr variable templates
- Authentication-results: sourceware.org; auth=none
- References: <53CCFBEF dot 9080804 at verizon dot net> <53CDD522 dot 9040508 at maniacsvault dot net> <53D172E4 dot 9020902 at redhat dot com> <53D242FD dot 70002 at maniacsvault dot net> <53D2CB20 dot 1090201 at redhat dot com> <53D352F7 dot 7040402 at maniacsvault dot net> <53D3D34A dot 9080009 at redhat dot com> <53D3D3D9 dot 9000102 at redhat dot com> <0d580e6525100d13a49b2c9327c727f8 at imap dot force9 dot net> <CANq5SytLmkHuCF916K_QG31PTovq1Hir5Q76dJmY8Zce76R0Cg at mail dot gmail dot com> <280a4c80527b753476c274f90ab92fa6 at imap dot force9 dot net> <53D5B03D dot 1010300 at maniacsvault dot net> <53D688DE dot 6020001 at redhat dot com> <53D78C0B dot 10701 at maniacsvault dot net> <53D9591B dot 9020402 at redhat dot com> <53D964B3 dot 4070201 at maniacsvault dot net> <53DA3C32 dot 1030600 at redhat dot com> <53DB6A37 dot 3040703 at maniacsvault dot net> <53E121D3 dot 8010209 at redhat dot com> <53E13955 dot 2010307 at oracle dot com> <53E2D7BC dot 9080705 at redhat dot com>
This patch prevents the initializer from being checked when it is
dependent. It also sets the type of a dependent template-id referring
to a variable template to be unknown, making such expressions type
dependent.
2014-08-07 Andrew Sutton <andrew.n.sutton@gmail.com>
* pt.c (lookup_template_variable): Make the type unspecified if
any template arguments are dependent.
* decl.c (cp_finish_decl): Don't check the initializer if it is
value-dependent.
New test included.
Andrew Sutton
On Wed, Aug 6, 2014 at 9:34 PM, Jason Merrill <jason@redhat.com> wrote:
> On 08/05/2014 04:06 PM, Paolo Carlini wrote:
>>
>> Great. I will double check but var-templ4.C fails for me with an ICE.
>
>
> Hunh, I wonder how I missed that.
>
> Here's what I'm checking in; we want to unset DECL_COMDAT for variable
> templates, too.
>
>
Index: gcc/testsuite/g++.dg/cpp1y/var-templ6.C
===================================================================
--- gcc/testsuite/g++.dg/cpp1y/var-templ6.C (revision 0)
+++ gcc/testsuite/g++.dg/cpp1y/var-templ6.C (revision 0)
@@ -0,0 +1,12 @@
+// { dg-options "-std=c++1y" }
+
+template<typename T>
+ constexpr bool Class = __is_class(T);
+
+template<typename T>
+ constexpr bool Test = Class<T>;
+
+struct S { };
+
+static_assert(!Test<int>, "");
+static_assert(Test<S>, "");
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c (revision 213667)
+++ gcc/cp/decl.c (working copy)
@@ -6417,19 +6417,20 @@ cp_finish_decl (tree decl, tree init, bo
DECL_INITIAL (decl) = NULL_TREE;
}
+ bool value_dependent_p = init && value_dependent_init_p (init);
+
/* Generally, initializers in templates are expanded when the
template is instantiated. But, if DECL is a variable constant
then it can be used in future constant expressions, so its value
must be available. */
-
- if (!VAR_P (decl) || dependent_type_p (type))
+ if (!VAR_P (decl) || type_dependent_p || value_dependent_p)
/* We can't do anything if the decl has dependent type. */;
else if (init
&& init_const_expr_p
&& !type_dependent_p
&& decl_maybe_constant_var_p (decl)
&& !type_dependent_init_p (init)
- && !value_dependent_init_p (init))
+ && !value_dependent_p)
{
/* This variable seems to be a non-dependent constant, so process
its initializer. If check_initializer returns non-null the
Index: gcc/cp/pt.c
===================================================================
--- gcc/cp/pt.c (revision 213667)
+++ gcc/cp/pt.c (working copy)
@@ -8260,13 +8260,23 @@ lookup_template_class (tree d1, tree arg
return ret;
}
-/* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
+/* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
+ If the ARGLIST refers to any template parameters, the type of the
+ expression is the unknown_type_node since the template-id could
+ refer to an explicit or partial specialization.
+*/
tree
lookup_template_variable (tree templ, tree arglist)
{
- return build2 (TEMPLATE_ID_EXPR, TREE_TYPE (templ), templ, arglist);
+ tree type;
+ if (uses_template_parms (arglist))
+ type = unknown_type_node;
+ else
+ type = TREE_TYPE (templ);
+ return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
}
+
struct pair_fn_data
{