nested classes and friend declaration

Discussion in 'C++' started by Alexander Stippler, Jul 2, 2003.

  1. Hi

    what's wrong about the following code? It does not compile.

    template <typename T>
    class A {
    public:
    class B;
    };

    template <typename T>
    bool
    operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs);

    template <typename T>
    bool
    operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs);

    template <typename T>
    class A<T>::B
    {
    public:
    friend bool
    operator==<>(const B &lhs, const B &rhs);

    friend bool
    operator!=<>(const B &lhs, const B &rhs);
    };

    template <typename T>
    bool
    operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
    {
    return true;
    }

    template <typename T>
    bool
    operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
    {
    return false;
    }

    int
    main()
    {
    A<int>::B b;
    b == b;

    }

    regards,
    Alex
     
    Alexander Stippler, Jul 2, 2003
    #1
    1. Advertising

  2. "Alexander Stippler" <-ulm.de> wrote...
    > what's wrong about the following code? It does not compile.


    Several things are wrong. However, I'd like to know what prevented
    you from posting the compiler diagnostic?

    >
    > template <typename T>
    > class A {
    > public:
    > class B;
    > };
    >
    > template <typename T>
    > bool
    > operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs);
    >
    > template <typename T>
    > bool
    > operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs);
    >
    > template <typename T>
    > class A<T>::B
    > {
    > public:
    > friend bool
    > operator==<>(const B &lhs, const B &rhs);


    If you need a particular instantiation to be a friend, you expect that
    'T' is going to be determined here. It won't. The language does not
    allow template argument deduction from nested types in templates. You
    have to specify the T:

    friend bool operator==<T>(...

    ..

    >
    > friend bool
    > operator!=<>(const B &lhs, const B &rhs);


    Same here.

    > };
    >
    > template <typename T>
    > bool
    > operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
    > {
    > return true;
    > }
    >
    > template <typename T>
    > bool
    > operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
    > {
    > return false;
    > }
    >
    > int
    > main()
    > {
    > A<int>::B b;
    > b == b;


    Now, this is not going to be resolved. Again, because the compiler
    does not know how to deduce type arguments from nested types, you
    need to help it:

    operator==<int>(b,b);

    Ugly? Yes. But there is no other way. Perhaps you need to think
    of making your operator== and operator!= templates based on B and
    not on T:

    template<typename B> bool operator==(B const&, B const&);

    and then specialise them for A<T>::B if needed...

    >
    > }
    >
    > regards,
    > Alex
     
    Victor Bazarov, Jul 2, 2003
    #2
    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. Susan Baker
    Replies:
    1
    Views:
    383
    Victor Bazarov
    Jul 7, 2005
  2. request@no_spam.com
    Replies:
    5
    Views:
    444
  3. Replies:
    3
    Views:
    480
    Victor Bazarov
    Dec 7, 2007
  4. Chameleon

    friend and nested classes

    Chameleon, Feb 14, 2009, in forum: C++
    Replies:
    1
    Views:
    267
    Chameleon
    Feb 14, 2009
  5. Peter
    Replies:
    2
    Views:
    287
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page