operator== for nested classes.

A

ali_tofigh

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!
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

Victor Bazarov

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.

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
 
G

Gianni Mariani

template<class S>
bool operator==(const typename X<S>::Y &y1,
const typename X<S>::Y &y2)

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.
 
K

Kai-Uwe Bux

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
 
A

ali_tofigh

Thanks everyone for your replies. I changed the operator== to a member
function and things work nicely.

Best regards,
ALiX
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top