Templated friend classes

R

Ralf Goertz

Hi,

while I was redesigning some of my programs to have them resemble cool
C++ code my compiler gave me a hard time. For instance, I use a
templated base class and two derived classes. These need to know about
each others private internals so that I decided to make them friends of
each other. But in order to do that I need a forward declaration of one
of the classes. I couldn't find out how to do that correctly. The code
below doesn't compile, g++ says "error: template argument required for
'struct Derived2'". But if I do it like in (**) it says "error:
'Derived2' is not a template". I tried almost all word permutations of

friend template class Derived2<T>

for line (*) but I had no success. So I ended up making both classes
public which is bad. What is the correct way of doing this?

template <class ID_TYPE> class Base
{
ID_TYPE id;
};

template <class T> class Derived1: public Base<T>
{
friend class Derived2; // (*)
};

template <class T> class Derived2: public Base<T>
{
friend class Derived1<T>; (**)
};

int main()
{
Derived1<int> d1;
return 0;
}
 
K

kwikius

Ralf said:
Hi,

while I was redesigning some of my programs to have them resemble cool
C++ code my compiler gave me a hard time. For instance, I use a
templated base class and two derived classes. These need to know about
each others private internals so that I decided to make them friends of
each other. But in order to do that I need a forward declaration of one
of the classes. I couldn't find out how to do that correctly. The code
below doesn't compile, g++ says "error: template argument required for
'struct Derived2'". But if I do it like in (**) it says "error:
'Derived2' is not a template". I tried almost all word permutations of

friends and templates are weird.

Anyone following works in VC8 and IIRC gcc4. But I think there are
issues e.g different namespace that can screw things up in gcc ans
sometimes VC and gcc dont agree on syntax


template <class T > class Derived;

template <class ID_TYPE> class Base
{
ID_TYPE id;

private:
friend class Derived<ID_TYPE>;
int dummy;
};

template <class T> class Derived: public Base<T>
{
// (*)
// check it allows access
public:
Derived(){ Base<T>::dummy = 1;}
};



int main()
{
Derived<int> d1;
return 0;
}
 
R

Ralf Goertz

Victor said:
I would hope that the syntax

template<class T> class Derived1: public Base<T>
{
...
template<> friend class Derived2<T>;
};

or

template<class T> class Derived1: public Base<T>
{
...
template friend class Derived2<T>;
};

would work, but IIRC they don't.

Yes, you're right, they don't. At least not with my compiler. Is there a
reason for this? I also tried to use a forward declaration of Derived2
before the definition of Derived1 but obviously, those declaration
aren't allowed for templated classes, either. Again, I don't see why. If
a non-templated class is used in a forward definition, the compiler also
doesn't know what types are in that class.

Ralf
 
R

Ralf Goertz

kwikius said:
template <class T > class Derived;

Ah, that's the trick. I thought I'd need to specify that Derived2 is
derived from "Base".

Thanks,

Ralf
 
R

Ralf Goertz

Ralf Goertz wrote:

I also tried to use a forward declaration of Derived2 before the
definition of Derived1 but obviously, those declaration aren't allowed
for templated classes, either.

That's nonsense. I didn't do it right (I added the ": public Base<T>"
part to the forwarding declaration). Sorry for the noise.

Ralf
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top