friendship with nested class

M

Mr Dyl

I'm trying to declare the following friendship and VS.Net 2003 is
complaining:


template <class T>
class Outter
{
class Inner {...}
...
}

class A
{
template <class T> friend class Outter;
template <class T> friend class Outter<T>::Inner;
}


On the second friend declaration I get the error "'Outter::Inner'
cannot be redeclared in the current scope".
Any ideas? Is this a syntax issue or does the standard actually not
allow this?

Many thanks!!
 
S

Sunil Varma

C++ compiler assumes that while accessing any member of the template
class, the class needs to be instantiated.
That's the type needs to known at compile time.

Ex.--

Try with something like.

friend class Outter<int>::Inner;

this works fine.
 
M

Mr Dyl

Sorry, I should have mentioned that this isn't possible because the
type for the template argument of Outter is unknown at this point :(
Technically I could forward declare every possibility and then declare
friendship with each but that's quite a hack and not terribly portable.

I was hoping there'd be some way of declaring friendship with Inner for
all template instantiations of Outter as is done with the first
friendship statement:

template <class T> friend class Outter;

Impossible????

Thx!
 
A

Alf P. Steinbach

* Mr Dyl:
I'm trying to declare the following friendship and VS.Net 2003 is
complaining:


template <class T>
class Outter
{
class Inner {...}
...
}

class A
{
template <class T> friend class Outter;
template <class T> friend class Outter<T>::Inner;
}


On the second friend declaration I get the error "'Outter::Inner'
cannot be redeclared in the current scope".
Any ideas? Is this a syntax issue or does the standard actually not
allow this?

Since class Inner is private you can't refer to it. However, making it
public gives errors with MSVC 7.1 and MinGW g++ 3.4.4, while compiling
fine with Comeau Online 4.3.3.

The standard allows specifying a class nested in a template class as a
friend (of a non-template class), and even gives a direct example in
paragraph 14.5.3/6. That example, with a few callable functions added:

template<class T> struct A {
struct B { void foo(); }; // 'foo' added by me
void f();
};

class C {
template<class T> friend struct A<T>::B;
template<class T> friend void A<T>::f();

static void sayNoMore() {} // 'sayNoMore' added by me
};

// Following added by me:

template<class T> void A<T>::B::foo()
{
C::sayNoMore();
}

int main()
{
A<int>::B o;
o.foo();
}

This example from the standard has the same compilation problems as your
code did.

A workaround could perhaps be to move the nested class out, and if
necessary provide a typedef in the class it's nested in.
 
E

Ed

You could make class A a template as well. This would allow you to
instantiate A with any type.

A<int> a1;
A<MyClass> a2
and so on.

template <typename T>
class A {
friend class Outer<T>;
friend class Outer<T>::Inner<T>;
};
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top