Template Methods.

Gabriel Dos Reis Gabriel.Dos-Reis@dptmaths.ens-cachan.fr
Sat Oct 10 14:04:00 GMT 1998

>>>>> «Erez», Erez Louidor Lior <s3824888@techst02.technion.ac.il> wrote:

Erez> I was working with templates and discovered some strange behaviour
Erez> from egcs.
Erez> I was using the October 5th snapshot.

Erez> 1. How can I declare a template function with two parameters to be a
Erez> friend of a class C, in such a way that only one of the template
Erez> parameters
Erez> is fixed, and the other is a template argument. I tried the following
Erez> but
Erez> it did not work:

Erez> template <typename T, typename S>
Erez> void foo();

Erez> class bar{
Erez>   template <typename S>
Erez>   friend void foo<S, int>();
Erez> };

Erez> egcs complained:
Erez> "templates2.cc:6: template-id `foo<S, int>' in declaration of primary
Erez> template"

Erez> 2. How can I do the same with classes:
Erez> When I tried the following:

Erez> template <class T, class B>
Erez> class A{
Erez> };

Erez> class B{
Erez>   template <class U>
Erez>   friend class A<U, int>;
Erez> };

Erez> egcs complained:
Erez> "templates3.cc:7: partial specialization `A<U,int>' declared `friend'"

In the two cases, EGCS is right: the Standard does not allow friend
declarations to be *partial* specializations though they can refer to
specializations, that is

	class B { friend class A<double, int>; }; 

is allowed.

The only workaround I can think of is to define a helper template:

	template<class U> struct A_helper : A<U, int> {};

	class B {
		template<class U> friend class A_helper<U>;

Hope this helps

-- Gaby
"One reason that life is complex is that it has a 
real part and imaginary part." -- Andrew Koenig

More information about the Gcc-bugs mailing list