PATCH for default arguments in templates

Mark Mitchell mmitchell@usa.net
Wed Apr 1 15:14:00 GMT 1998


Jason --

  Here's a patch to improve the handling of default arguments for
template functions.

-- 
Mark Mitchell <mmitchell@usa.net>
http://home.earthlink.net/~mbmitchell
Consulting Services Available

Wed Apr  1 14:58:35 1998  Mark Mitchell  <mmitchell@usa.net>

	* call.c (build_over_call): Do name resolution for default
	arguments of function templates in the scope of the templates.

Index: call.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/call.c,v
retrieving revision 1.58
diff -c -p -r1.58 call.c
*** call.c	1998/03/31 13:25:11	1.58
--- call.c	1998/04/01 22:53:45
*************** build_over_call (fn, convs, args, flags)
*** 3220,3228 ****
        tree arg = TREE_PURPOSE (parm);
  
        if (DECL_TEMPLATE_INFO (fn))
! 	/* This came from a template.  Instantiate the default arg here,
! 	   not in tsubst.  */
! 	arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
        converted_args = expr_tree_cons
  	(NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
  	 converted_args);
--- 3220,3245 ----
        tree arg = TREE_PURPOSE (parm);
  
        if (DECL_TEMPLATE_INFO (fn))
! 	{
! 	  /* This came from a template.  Instantiate the default arg here,
! 	     not in tsubst.  In the case of something like:
! 
! 	       template <class T>
! 	       struct S {
! 	         static T t();
! 		 void f(T = t());
! 	       };
! 
! 	     we must be careful to do name lookup in the scope of
! 	     S<T>, rather than in the current class.  */
! 	  if (DECL_REAL_CONTEXT (fn))
! 	    pushclass (DECL_REAL_CONTEXT (fn), 2);
! 
! 	  arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
! 
! 	  if (DECL_REAL_CONTEXT (fn))
! 	    popclass (0);
! 	}
        converted_args = expr_tree_cons
  	(NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
  	 converted_args);
Index: gcc/testsuite/g++.old-deja/g++.pt/defarg4.C
===================================================================
RCS file: defarg4.C
diff -N defarg4.C
*** /dev/null	Mon Dec 31 20:00:00 1979
--- defarg4.C	Wed Apr  1 14:56:31 1998
***************
*** 0 ****
--- 1,32 ----
+ // Build don't link:
+ 
+ template <class T>
+ struct S1
+ {
+   void foo(T = t());
+ 
+   static T t();
+ };
+ 
+ 
+ template <class T>
+ struct S2
+ {
+   void bar();
+ };
+ 
+ 
+ template <class T>
+ void S2<T>::bar ()
+ {
+   S1<T> st;
+   st.foo();
+ }
+ 
+ 
+ int main()
+ {
+   S2<int> s2i;
+   s2i.bar();
+ }
+ 



More information about the Gcc-bugs mailing list