From: Martin T. on
Hi all!

Given:

struct agg {
int x;
int y;
double z;
std::string tag;
std::vector<long> data;
// ...
};

Is there any way in C++ to automatically generate a memberwise
operator== / operator!= or do you always have to code these operators
yourself?
(I don't currently see the requirement to actually have such a member
wise comparison in my production code. However, for writing some unit
tests, it would be extremely handy ...)

cheers,
Martin


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thomas Matthews on
Martin T. wrote:
> Hi all!
>
> Given:
>
> struct agg {
> int x;
> int y;
> double z;
> std::string tag;
> std::vector<long> data;
> // ...
> };
>
> Is there any way in C++ to automatically generate a memberwise
> operator== / operator!= or do you always have to code these operators
> yourself?
> (I don't currently see the requirement to actually have such a member
> wise comparison in my production code. However, for writing some unit
> tests, it would be extremely handy ...)
>
> cheers,
> Martin
>
>

There is no automatic generation of equality operators.
The Boost Library provides equality_comparable which will
generate the operator!=() if you provide an operator==().
Also look up less_than_comparable.

I just accept that creating equality operators is part
of the coding time and move on.

If you are really paranoid about saving coding time,
you could invest some time and write a program to
generate all the getters, setters, equality and
comparison methods based on the members. It is a good
exercise to learn C++ and Object Oriented Design.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: tohava on
On Sep 5, 3:36 pm, Thomas Matthews
<Thomas_Really_Hates_S...(a)matthews.cox.net> wrote:
>
> I just accept that creating equality operators is part
> of the coding time and move on.

I think that part of the problem with this is that whenever a new
member is added to the class, one must remember to update operator ==.
Forgetting this can lead to bad behavior. Hence, I do not think this
is just about saving coding time.

>
> If you are really paranoid about saving coding time,
> you could invest some time and write a program to
> generate all the getters, setters, equality and
> comparison methods based on the members. It is a good
> exercise to learn C++ and Object Oriented Design.

Once again, he would have to re-run the program every time he adds a
member (not to mention the problem of handling C++'s syntax).



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 5 sep, 01:19, "Martin T." <0xCDCDC...(a)gmx.at> wrote:

> Is there any way in C++ to automatically generate a memberwise
> operator== / operator!=

No.

> or do you always have to code these operators
> yourself?

Yes. It might be easier if you use some kind of tuple framework to
generate it though.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Balog Pal on

"tohava" <tohava(a)gmail.com>
>> I just accept that creating equality operators is part
>> of the coding time and move on.
>
> I think that part of the problem with this is that whenever a new
> member is added to the class, one must remember to update operator ==.
> Forgetting this can lead to bad behavior. Hence, I do not think this
> is just about saving coding time.

Now, what is the percentage of classes/structs in a project that has a
meaningful op==?
And then, where it is based on full memberwise comparition?

Guess if you find the special case, it will not change members all that
often...



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]