template parameters as friends

J

John Harrison

Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).


template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}

john
 
G

Gianni Mariani

John said:
Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).


template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}

This does not do quite what I think you want it to do but it's what I've
used in some cases.

template <class X> class I;

template <class D>
class I_Accessor
{
protected:
I<D> f() { return I<D>(); }
};

template <class X> class I
{
friend class I_Accessor<X>;
private:
I() {}
};

class C
: I_Accessor<C>
{
public:
I<C> f() { return I_Accessor<C>::f(); }
};

int main()
{
C c;
c.f();
}
 
I

Ian

John said:
Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).


template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}
It should be, shouldn't it? It compiles fine for me.

Could it depend on when the compiler parses I? If this is done when
instantiating C, X (type C) is known. If not, X could be anything, for
example

class D
{
public:
I<int> f() { return I<int>(); }
};

Will fail as an int can't be a friend.

Ian
 
J

John Harrison

Gianni said:
This does not do quite what I think you want it to do but it's what I've
used in some cases.

template <class X> class I;

template <class D>
class I_Accessor
{
protected:
I<D> f() { return I<D>(); }
};

template <class X> class I
{
friend class I_Accessor<X>;
private:
I() {}
};

class C
: I_Accessor<C>
{
public:
I<C> f() { return I_Accessor<C>::f(); }
};

int main()
{
C c;
c.f();
}

That's clever, I can make that work, thanks.

john
 
J

John Harrison

Ian said:
It should be, shouldn't it? It compiles fine for me.

Which compiler? I tried VC++ 7.1, which just ignored the friend
declaration, so I got an access error, and g++ 3.4.4 which complained
about the friend declaration.
Could it depend on when the compiler parses I? If this is done when
instantiating C, X (type C) is known. If not, X could be anything, for
example

class D
{
public:
I<int> f() { return I<int>(); }
};

Will fail as an int can't be a friend.

Ian

The compiler has to parse a template twice. Once when it is first read
and once when it is instantiated. But exactly what should be done when
is not something I'm very familar with.

But now I'm thinking my code is probably just a syntax error. X isn't a
class name, its a template parameter so 'friend class X' is just wrong.
I guess I should try looking at the C++ standard, but asking here is
much easier.

john
 
I

Ian

John said:
Which compiler? I tried VC++ 7.1, which just ignored the friend
declaration, so I got an access error, and g++ 3.4.4 which complained
about the friend declaration.
Sun CC.

The compiler has to parse a template twice. Once when it is first read
and once when it is instantiated. But exactly what should be done when
is not something I'm very familar with.
It depends how much checking it does when it is first read. I know that
gcc does more than the Sun compiler does, I think it parses all member
functions, where as the Sun compiler only does this when they are
instantiated.
But now I'm thinking my code is probably just a syntax error. X isn't a
class name, its a template parameter so 'friend class X' is just wrong.
I guess I should try looking at the C++ standard, but asking here is
much easier.
This could be a good one for comp.std.c++.

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top