friendship with nested class

Discussion in 'C++' started by Mr Dyl, Nov 25, 2005.

  1. Mr Dyl

    Mr Dyl Guest

    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!!
     
    Mr Dyl, Nov 25, 2005
    #1
    1. Advertising

  2. Mr Dyl

    Sunil Varma Guest

    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.
     
    Sunil Varma, Nov 25, 2005
    #2
    1. Advertising

  3. Mr Dyl

    Mr Dyl Guest

    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!
     
    Mr Dyl, Nov 25, 2005
    #3
  4. * 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?
     
    Alf P. Steinbach, Nov 25, 2005
    #4
  5. Mr Dyl

    Ed Guest

    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>;
    };
     
    Ed, Nov 25, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,848
    Thomas Weidenfeller
    Oct 12, 2005
  2. Thomas Matthews

    Circular Class Template Friendship

    Thomas Matthews, Jul 24, 2004, in forum: C++
    Replies:
    7
    Views:
    527
    Victor Bazarov
    Jul 28, 2004
  3. Kai Wu
    Replies:
    2
    Views:
    305
    Matt Bitten
    Mar 2, 2005
  4. Juha Nieminen

    Friendship and nested classes

    Juha Nieminen, Jun 13, 2008, in forum: C++
    Replies:
    2
    Views:
    345
    Martin York
    Jun 13, 2008
  5. Qi
    Replies:
    4
    Views:
    780
Loading...

Share This Page