473,465 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

operator== for nested classes.

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;
}
------------------------------------------------------------

Feb 12 '06 #1
4 1556
al********@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
Feb 12 '06 #2
al********@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.

{
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;
}
------------------------------------------------------------

Feb 12 '06 #3
al********@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
Feb 12 '06 #4
Thanks everyone for your replies. I changed the operator== to a member
function and things work nicely.

Best regards,
ALiX

Feb 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
1
by: Eddie Parker | last post by:
Hi! I'm having an interesting problem that I can't seem to get to work, and I'm curious if someone could either a) tell me how to make it work, or b) tell me why it *can't* work. :) Anyhow,...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.