template overloading / nested class

A

Alexander Stippler

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
 
V

Victor Bazarov

Alexander Stippler said:
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
 
M

Michael Kochetkov

Alexander Stippler said:
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.
 
R

Rob Williscroft

Alexander Stippler wrote in
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top