template overloading / nested class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Stippler

    template overloading / nested class

    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==(cons t 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<doubl e> ab2;

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

    regards,
    alex
  • Michael Kochetkov

    #2
    Re: template overloading / nested class


    "Alexander Stippler" <stip@mathemati k.uni-ulm.de> wrote in message
    news:3f1fde12@n ews.uni-ulm.de...[color=blue]
    > 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==(cons t 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<doubl e> ab2;
    >
    > std::cerr << (ab1 == ab2) << std::endl;[/color]
    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<T 1>::template_na me<T2> is missing.

    --
    With regards,
    Michael Kochetkov.


    Comment

    • Victor Bazarov

      #3
      Re: template overloading / nested class

      "Alexander Stippler" <stip@mathemati k.uni-ulm.de> wrote...[color=blue]
      > 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==(cons t 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<doubl e> ab2;
      >
      > std::cerr << (ab1 == ab2) << std::endl;
      > return 0;
      > }[/color]

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

      Victor


      Comment

      • Rob Williscroft

        #4
        Re: template overloading / nested class

        Alexander Stippler wrote in news:3f1fde12@n ews.uni-ulm.de:
        [color=blue]
        >
        > Why is there no matching operator== in the following code?
        >
        >[/color]

        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<doubl e> ab2;

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

        HTH

        Rob.
        --

        Comment

        Working...