Declaration inside template

Kriang Lerdsuwanakij lerdsuwa@scf-fs.usc.edu
Thu Sep 11 23:19:00 GMT 1997


Hi,

The current g++ still accepts all kind of declaration/definition in a
template.  Here is an example.

template <class T> extern "C" { unsigned f(T a) { return sizeof(a); } }

int main()
{
	f(1.0);
}


Is there any plan to limit declaration/definition to those suggested by
the ANSI draft (chapter 14, first paragraph) ?    The following patch
works for me.  There is also another change so that the sequence
'template<...> __extension__ ...' is no longer allowed (seems to be The
Right Thing).  '__extension__ template<...> ...' still works.



* parse.y (template_extdef): New nonterminal.
(extdef) Use it.
(templatedef) Use it.


--- cp-old/parse.y	Tue Sep  9 00:10:22 1997
+++ cp/parse.y	Thu Sep 11 19:32:53 1997
@@ -358,13 +358,17 @@
 		  pop_everything (); }
 	;
 
-extdef:
+template_extdef:
 	  fndef eat_saved_input
 		{ if (pending_inlines) do_pending_inlines (); }
 	| datadef
 		{ if (pending_inlines) do_pending_inlines (); }
 	| template_def
 		{ if (pending_inlines) do_pending_inlines (); }
+	;
+
+extdef:
+	  template_extdef
 	| asm_keyword '(' string ')' ';'
 		{ if (TREE_CHAIN ($3)) $3 = combine_strings ($3);
 		  assemble_asm ($3); }
@@ -482,7 +486,7 @@
 
 template_def:
 	  template_header
-	  extdef
+	  template_extdef
 		{ end_template_decl (); }
 	| template_header
 	  error  %prec EMPTY



More information about the Gcc mailing list