C++: Namespaces and in class defined friends

Martin v. Loewis martin@mira.isdn.cs.tu-berlin.de
Mon Jun 14 02:32:00 GMT 1999


> I stumbled across a problem with my numerical library in C++. I recently
> converted to using namespaces all over the place and while concetually
> simple, it seems there are problems.

Thanks for your bug report. The problem was that instantiation of the
template friend would overwrite the pointer to the global function.

Ok to install in both branches?

Regards,
Martin

1999-06-14  Martin von Löwis  <loewis@informatik.hu-berlin.de>

	* pt.c (tsubst_friend_function): Push into namespace of friend
	function before pushdecl'ing it.
	
void foo(){}

namespace Bar{
  template<class X>
    class Y{
      friend void foo(Y<X>){}
    };
}

int main()
{
  Bar::Y<int> y;
  foo(y);
  foo();
}

Index: pt.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/pt.c,v
retrieving revision 1.306
diff -c -p -r1.306 pt.c
*** pt.c	1999/05/25 15:31:36	1.306
--- pt.c	1999/06/14 09:05:25
*************** tsubst_friend_function (decl, args)
*** 4495,4500 ****
--- 4495,4501 ----
        tree old_decl;
        tree new_friend_template_info;
        tree new_friend_result_template_info;
+       tree ns;
        int  new_friend_is_defn;
  
        /* We must save some information from NEW_FRIEND before calling
*************** tsubst_friend_function (decl, args)
*** 4517,4523 ****
--- 4518,4530 ----
  	  new_friend_result_template_info = NULL_TREE;
  	}
  
+       /* Inside pushdecl_namespace_level, we will push into the 
+ 	 current namespace. However, the friend function should 
+ 	 tyically go into the namespace of the template. */
+       ns = decl_namespace_context (new_friend);
+       push_nested_namespace (ns);
        old_decl = pushdecl_namespace_level (new_friend);
+       pop_nested_namespace (ns);
  
        if (old_decl != new_friend)
  	{


More information about the Gcc-patches mailing list