I have a templated class with a nested class inside it. I'm trying to
define an equality operator for the nested class, one that will work
with STL-algorithms and containers. The code below shows the structure
of my program. I have tried several other approaches with no success.
Any insightful thoughts are most appreciated.
Regards,
ALiX
------------------------------------------------------------
#include <vector>
template <class T>
struct X {
T d;
struct Y {
int i;
};
};
template<class S>
bool operator==(const typename X<S>::Y &y1,
const typename X<S>::Y &y2)
{
return y1.i == y2.i;
}
int main()
{
X<double>::Y y1, y2;
y1 == y2; // this fails!
operator==<double>(y1, y2); // this works,
// but not when 'using namespace std'.
std::vector<X<double>::Y> yv1, yv2;
yv1.push_back(X<double>::Y());
yv2.push_back(X<double>::Y());
yv1 == yv2; // this fails!
return 0;
}
------------------------------------------------------------
Your example has non-deducible contex. The compiler cannot deduce
what 'S' is from the nested class. The deducible contexts are defined
in 14.8.2.4/9.
>
> Regards,
> ALiX
>
> ------------------------------------------------------------
> #include <vector>
>
> template <class T>
> struct X {
> T d;
> struct Y {
> int i;
> };
> };
>
> template<class S>
> bool operator==(const typename X<S>::Y &y1,
> const typename X<S>::Y &y2)
> {
> return y1.i == y2.i;
> }
>
> int main()
> {
> X<double>::Y y1, y2;
> y1 == y2; // this fails!
> operator==<double>(y1, y2); // this works,
> // but not when 'using namespace std'.
>
> std::vector<X<double>::Y> yv1, yv2;
>
> yv1.push_back(X<double>::Y());
> yv2.push_back(X<double>::Y());
>
> yv1 == yv2; // this fails!
>
> return 0;
> }
> ------------------------------------------------------------
V
--
Please remove capital As from my address when replying by mail
There is no way for the compiler to automatically deduce S (easily).
If you think about X<S>::Y may not be unique and so the compiler would
need to look at X<S>::Y for all possible S (where S is any type). In
theory, it could be done but it's going to be VERY slow since the
compiler would need to create X for every S.
There is a trick I use for regular functions but it does not work for
operators.
> Hi everyone,
>
> I have a templated class with a nested class inside it. I'm trying to
> define an equality operator for the nested class, one that will work
> with STL-algorithms and containers. The code below shows the structure
> of my program. I have tried several other approaches with no success.
> Any insightful thoughts are most appreciated.
>
> Regards,
> ALiX
>
> ------------------------------------------------------------
> #include <vector>
>
> template <class T>
> struct X {
> T d;
> struct Y {
> int i;
> };
> };
>
> template<class S>
> bool operator==(const typename X<S>::Y &y1,
> const typename X<S>::Y &y2)
> {
> return y1.i == y2.i;
> }
>
> int main()
> {
> X<double>::Y y1, y2;
> y1 == y2; // this fails!
[snip]
> }
> ------------------------------------------------------------
Others have already remarked that the compiler cannot deduce the template
argument from the composite. As a work-around, you can try implementing
operator== as a member function:
#include <vector>
template <class T>
struct X {
T d;
struct Y {
int i;
bool operator== ( Y const & other ) const {
return ( i == other.i );
}
};
};
int main()
{
X<double>::Y y1, y2;
y1 == y2; // this compiles!
}
Best
Kai-Uwe Bux
Best regards,
ALiX