Connecting Tech Pros Worldwide Forums | Help | Site Map

template overloading / nested class

Alexander Stippler
Guest
 
Posts: n/a
#1: Jul 19 '05
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

Michael Kochetkov
Guest
 
Posts: n/a
#2: Jul 19 '05

re: template overloading / nested class



"Alexander Stippler" <stip@mathematik.uni-ulm.de> wrote in message
news:3f1fde12@news.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==(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;[/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<T1>::template_name<T2> is missing.

--
With regards,
Michael Kochetkov.


Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 19 '05

re: template overloading / nested class


"Alexander Stippler" <stip@mathematik.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==(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;
> }[/color]

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

Victor


Rob Williscroft
Guest
 
Posts: n/a
#4: Jul 19 '05

re: template overloading / nested class


Alexander Stippler wrote in news:3f1fde12@news.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<double> ab2;

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

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Closed Thread