Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

operator== for nested classes.

9 views
Skip to first unread message

ali_t...@hotmail.com

unread,
Feb 12, 2006, 12:17:18 PM2/12/06
to
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;
}
------------------------------------------------------------

Victor Bazarov

unread,
Feb 12, 2006, 1:18:09 PM2/12/06
to
ali_t...@hotmail.com wrote:
> 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
--
Please remove capital As from my address when replying by mail


Gianni Mariani

unread,
Feb 12, 2006, 1:26:16 PM2/12/06
to
ali_t...@hotmail.com wrote:
...

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

Kai-Uwe Bux

unread,
Feb 12, 2006, 4:13:14 PM2/12/06
to
ali_t...@hotmail.com wrote:

> 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

ali_t...@hotmail.com

unread,
Feb 13, 2006, 3:26:40 AM2/13/06
to
Thanks everyone for your replies. I changed the operator== to a member
function and things work nicely.

Best regards,
ALiX

0 new messages