Template Friend class

O

Olumide

Hi,

I've got a class 'Runner' that declares a template class 'Corner' as
friend as follows:

template <typename T> class Corner
{
friend class T;

public:
Corner<T>( T& object );

private:
T& m_object;
};

template <typename T> Corner<T>::Corner( T& object ) : m_object
( object )
{
}

////////////////////////////////////////////////

class Runner
{
public:
template <typename Runner> friend class Corner;

void compute()
{
//Corner<Runner >( *this ); // causes linker error
}
};


The code compiles, but fails at the linking stage with the message:
unresolved external symbol "public: __thiscall Corner<class
Runner>::Corner<class Runner>(class Runner &)".

I'm working with Visual Studio .NET 2003.

Thanks,

- Olumide


(I'm using VS .NET 2003), but links
 
V

Victor Bazarov

Olumide said:
I've got a class 'Runner' that declares a template class 'Corner' as
friend as follows:

template <typename T> class Corner
{
friend class T;

I think it's explicitly prohibited by the Standard. But I don't
remember where I got the notion.

V
 
O

Olumide

I think it's explicitly prohibited by the Standard.  But I don't
remember where I got the notion.

I've solved the problem. I just had to include the template class
definitions in the header file. I'm not exactly sure why tho' ... I
suppose its because the entire template class definition needs to be
available in order for the compiler to generate type specific code as
needed. As the entire class definition was not available, no code was
generated, thus the linker error.
 
V

Victor Bazarov

Olumide said:
I've solved the problem. I just had to include the template class
definitions in the header file. I'm not exactly sure why tho' ... I
suppose its because the entire template class definition needs to be
available in order for the compiler to generate type specific code as
needed. As the entire class definition was not available, no code was
generated, thus the linker error.

Oh, *that*... It's in the FAQ. Have you read the FAQ?

V
 
M

Maxim Yegorushkin

I think it's explicitly prohibited by the Standard. But I don't remember
where I got the notion.

T can be a fundamental type, like void or int. friend class int is not
allowed.
 
M

Maxim Yegorushkin

I don't think that explanation makes sense.

Interesting, you got me thinking! ;)

A friend can fundamentally be a scope. Either function scope or
struct/class scope in current C++ (it would be nice to have friend
namespaces along with template namespaces though). Thus there is syntax
friend <function> and friend struct/class <class>, since there can be a
function and a class with the same name, so it needs disambiguation. A
fundamental type has no scope, so there is no syntax to make a
fundamental type a friend.

The standard could allow friend class T and in cases where T is a
fundamental type the compiler could report an error. I don't know why
they did not do so ;)
 

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,077
Latest member
SangMoor21

Latest Threads

Top