By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,408 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,408 IT Pros & Developers. It's quick & easy.

friendship with nested class

P: n/a
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!!

Nov 25 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Nov 25 '05 #2

P: n/a
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!

Nov 25 '05 #3

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 25 '05 #4

P: n/a
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>;
};

Nov 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.