Patch for constexpr variable templates

Braden Obrzut admin@maniacsvault.net
Sun Jul 13 04:07:00 GMT 2014


Here is a patch that expands on Gabriel Dos Reis's initial var-template 
patch ( https://gcc.gnu.org/ml/gcc-patches/2013-03/msg01295.html ).  I 
just noticed now that those patches were committed to a branch, so if 
need be I can resubmit the patch with Gaby's changes not included.

The purpose of the patch is to implement the subset of variable 
templates that is a prerequisite for concept variables in the 
c++-concepts branch.  With that said, only constexpr variables are 
supported (both in namespace and class scopes).  There is no handling 
for generating symbols for these variables so they are only supported in 
contexts where they can be evaluated at compile time (I believe that 
would be only as an rvalue).  On that note, explicit instantiations of 
variable templates do work, but will generate duplicate symbol errors if 
more than one exists in the file.  Other than those issues, the should 
work as expected.

I would like to call attention to the changes in parser.c.  Andrew 
pointed out to me that it may be a good idea to have 
cp_parser_template_id return the instantiated VAR_DECL directly instead 
of deferring it to cp_parser_postfix_expression like function 
templates.  Since this would produce a new possible return type for the 
function, I've found that would require more widespread changes 
(although I can't accurately say by how much).  By deferring the 
instantiation, some code can be shared between function templates and 
variable templates.

2014-07-12  Braden Obrzut  <admin@maniacsvault.net>

     * decl.c (grokvardecl): Handle specializations of variable templates.
     (grokdeclarator): Handle variable template id expressions.
     * decl2.c (check_member_template): Allow declaration of template member
     variables.
     (grokfield): Assign class context to template member variables in order
     that variable_template_p to detect them properly.
     * parser.c (cp_parser_postfix_expression): Resolve VAR_DECLs from
     TEMPLATE_ID_EXPRs.
     (cp_parser_template_id): Build a TEMPLATE_ID_EXPR for variable 
templates.
     * pt.c (register_specialization): Accept variable templates.
     (determine_specialization): Accept variable templates.
     (check_template_variable): Fixed wanted template header count.
     (lookup_template_variable): New.
     (instantiate_template_1): Evaluate initializer for variable templates.
     (do_decl_instantiation): Handle templat variables.
     (instantiate_decl): Handle template variables.
     * semantics.c (finish_template_variable): New.

2014-07-12  Braden Obrzut  <admin@maniacsvault.net>

     * g++.dg/template/var-templ1.C: New.
     * g++.dg/template/var-templ2.C: New.
     * g++.dg/template/var-templ3.C: New.
     * g++.dg/template/var-templ4.C: New.

2013-03-29  Gabriel Dos Reis  <gdr@integrable-solutions.net>

     * cp-tree.h (variable_template_p): Do not check scope.
     * pt.c (check_template_variable): Fix thinko from previous change.
     (push_template_decl_real): Fix formatting.


2013-03-29  Gabriel Dos Reis  <gdr@integrable-solutions.net>

     * cp-tree.h (variable_template_p): New.
     * pt.c (check_template_variable): Accept variable temploids at
     non-class scope.
     (push_template_decl_real): The current instantiation of a template
     can be a VAR_DECL.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: var-templ.diff
Type: text/x-patch
Size: 17201 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20140713/5a574675/attachment.bin>


More information about the Gcc-patches mailing list