Re: operator== for aggregate types / member wise comparison
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! ]