template overloading / nested class

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

  1. Hi,

    Why is there no matching operator== in the following code?

    #include <iostream>

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

    template <class T>
    template <class S>
    class A<T>::B
    {
    };

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

    int
    main()
    {
    A<int>::B<int> ab1;
    A<int>::B<double> ab2;

    std::cerr << (ab1 == ab2) << std::endl;
    return 0;
    }

    regards,
    alex
    Alexander Stippler, Jul 24, 2003
    #1
    1. Advertising

  2. "Alexander Stippler" <-ulm.de> wrote...
    > Why is there no matching operator== in the following code?
    >
    > #include <iostream>
    >
    > template <class T>
    > class A
    > {
    > public:
    > template <class S>
    > class B;
    > };
    >
    > template <class T>
    > template <class S>
    > class A<T>::B
    > {
    > };
    >
    > template <class T, class A1, class A2>
    > bool
    > operator==(const typename A<T>::template B<A1> &lhs,
    > const typename A<T>::template B<A2> &rhs)
    > {
    > return true;
    > }
    >
    > int
    > main()
    > {
    > A<int>::B<int> ab1;
    > A<int>::B<double> ab2;
    >
    > std::cerr << (ab1 == ab2) << std::endl;
    > return 0;
    > }


    I believe that behaviour is according with 14.8.2.4/4. Nested
    types cannot be deduced.

    Victor
    Victor Bazarov, Jul 24, 2003
    #2
    1. Advertising

  3. "Alexander Stippler" <-ulm.de> wrote in message
    news:-ulm.de...
    > Hi,
    >
    > Why is there no matching operator== in the following code?
    >
    > #include <iostream>
    >
    > template <class T>
    > class A
    > {
    > public:
    > template <class S>
    > class B;
    > };
    >
    > template <class T>
    > template <class S>
    > class A<T>::B
    > {
    > };
    >
    > template <class T, class A1, class A2>
    > bool
    > operator==(const typename A<T>::template B<A1> &lhs,
    > const typename A<T>::template B<A2> &rhs)
    > {
    > return true;
    > }
    >
    > int
    > main()
    > {
    > A<int>::B<int> ab1;
    > A<int>::B<double> ab2;
    >
    > std::cerr << (ab1 == ab2) << std::endl;

    I will take a risk to suppose a compiler cannot deduce template arguments.
    All forms for which a compiler can deduce template type argument are listed
    in 14.8.2.4/9. template_name<T1>::template_name<T2> is missing.

    --
    With regards,
    Michael Kochetkov.
    Michael Kochetkov, Jul 24, 2003
    #3
  4. Alexander Stippler wrote in news:-ulm.de:

    >
    > Why is there no matching operator== in the following code?
    >
    >


    Victor and Michael have already answered that.

    I rewrote you code with a member operator==(), and it compiles
    fine on 2/3 of the compilers I tested. Is there some reason
    you need operator==() to be a non-member ?

    #include <iostream>
    #include <ostream>

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

    template <class T>
    template <class S>
    class A<T>::B
    {
    public:

    template <typename A2>
    bool operator==(B<A2> const &rhs) const
    {
    return true;
    }
    };

    int main()
    {
    A<int>::B<int> ab1;
    A<int>::B<double> ab2;

    std::cerr << (ab1 == ab2) << std::endl;
    return 0;
    }

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jul 24, 2003
    #4
    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,755
    Thomas Weidenfeller
    Oct 12, 2005
  2. Gerhard Pfeiffer
    Replies:
    3
    Views:
    423
    Victor Bazarov
    Sep 14, 2006
  3. Replies:
    2
    Views:
    542
  4. claudiu
    Replies:
    2
    Views:
    298
    claudiu
    Apr 1, 2011
  5. claudiu
    Replies:
    3
    Views:
    495
    claudiu
    Apr 1, 2011
Loading...

Share This Page