Struct member equality without overloading operator== in C++ - c++

Is it possible to define some kind of template that can create a generic comparable operator for structs?
For example is it possible for something like this?
struct A
{
int one;
int two;
int three;
};
bool AreEqual()
{
A a {1,2,3};
A b {1,2,3};
return ComparableStruct<A>(a) == ComparableStruct<A>(b);
}
All this does is a field by field comparison of the structs. You can assume all fields are of basic types or have overloaded operator==.
I have a lot of structs like this and it would save me a lot of time if I can just put it in a template or something for comparison rather than defining an operator== for every single struct. Thanks!
Update
It seems like this is not possible with C++. I wonder why this is voted out of C++ proposals, if anyone has the reasons let us know!
For solution that works with basic types only see solution by R Sahu.

Is it possible to define some kind of template that can create a generic comparable operator for structs?
If the struct has no padding, you can use:
template <typename T>
struct ComparableStruct
{
ComparableStruct(T const& a) : a_(a) {}
bool operator==(ComparableStruct const& rhs) const
{
return (std::memcmp(reinterpret_cast<char const*>(&a_), reinterpret_cast<char const*>(&rhs.a_), sizeof(T)) == 0);
}
T const& a_;
};
Better yet, you can use a function template.
template <typename T>
bool AreEqual(T cost& a, T const& b)
{
return (std::memcmp(reinterpret_cast<char const*>(&a), reinterpret_cast<char const*>(&b), sizeof(T)) == 0);
}
If the struct has any padding, there is no guarantee that use of std::memcmp will work to compare two objects.

Look at https://github.com/apolukhin/magic_get. This library can automagically generate comparison operators for some fairly simple structs.
#include <iostream>
#include <boost/pfr/flat/global_ops.hpp>
struct S {
char c;
int i;
double d;
};
int main() {
S s1{'a', 1, 100.500};
S s2 = s1;
S s3{'a', 2, 100.500};
std::cout << "s1 " << ((s1 == s2) ? "==" : "!=") << " s2\n";
std::cout << "s1 " << ((s1 == s3) ? "==" : "!=") << " s3\n";
}
// Produces
// s1 == s2
// s1 != s3

What you asking to do is to traverse through various struct and comparing members is my understanding.
Iterating over a struct
This seems that it can't be done with standard c++, but that thread gives some ideas on what libraries to use.
It's not clear from your question if all the structs have the same format or not, and I'm assuming they do not.

Related

Generic comparison operator for structs

In many of my unit tests I need to compare the contents of simple structs having only data members:
struct Object {
int start;
int stop;
std::string message;
}
Now, if I want to write something like:
CHECK(object1==object2);
I always have to implement:
bool operator==(const Object& lhs, const Object& rhs) {
return lhs.start==rhs.start && lhs.stop==rhs.stop && lhs.message=rhs.message;
}
Writing all these comparison functions becomes tedious, but is also prone to errors. Just imagine, what will happen if I add a new data member to Object, but the comparison operator will not be updated.
Then I remembered my knowledge in Haskell and the magic deriving(Eq) directive, which just generates a sane comparison function for free.
How, could I derive something similar in C++?
Happily, I figured out that C++17 comes with a generic operator== and that every struct should be easily convertible to an std::tuple by the virtue of std::make_tuple.
So I boldly tried the following:
#include <tuple>
#include <iostream>
#include <tuple>
template<typename T>
bool operator==(const T& lhs, const T& rhs)
{
auto leftTuple = std::make_tuple(lhs);
auto rightTuple = std::make_tuple(rhs);
return leftTuple==rightTuple;
}
struct Object
{
std::string s;
int i;
double d;
};
int main(int arg, char** args)
{
std::cout << (Object{ "H",1,2. } == Object{ "H",1,2. }) << std::endl;
std::cout << (Object{ "A",2,3. } == Object{ "H",1,2. }) << std::endl;
return EXIT_SUCCESS;
}
But, unfortunately it just doesn't compile and I really don't know why. Clang tells me:
main.cpp:11:18: error: use of overloaded operator '==' is ambiguous (with operand types
'std::tuple<Object>' and 'std::tuple<Object>')
return leftTuple==rightTuple;
Can I possibly fix this compile error to get my desired behavior?
No, since comparing tuples reverts to comparing the elements of the tuple, so leftTuple == rightTuple tries to compare two Objects which is not possible.
that every struct should be easily convertible to an std::tuple by the virtue of std::make_tuple
No, you'll just get a tuple with one element, the struct.
The trick is to use std::tie:
std::tie(lhs.mem1, lhs.mem2) == std::tie(rhs.mem1, rhs.mem2)
but that has the same problem as your original solution. Unfortunately C++17 doesn't have any facility to avoid this problemyou could write a macro :). But in C++20 you will be able to do:
struct Object
{
std::string s;
int i;
double d;
bool operator==(const Object &) const = default;
};
which will generate the correct comparison operators for Object.

Efficient and simple comparison operators for structs

The application I am working on currently has a large number structs which contain data which is input from various sources such as data bases and files. For example like this:
struct A
{
float val1;
std::string val2;
int val3;
bool operator < (const A& other) const;
};
For processing, these structs are stored up in STL-containers, such as maps and therefore need a comparison operator. These are all the same and using simple boolean logic they can be written like this:
bool A:operator < (const A& o) const {
return val1 < o.val1 ||
(val1 == o.val1 && ( val2 < o.val2 ||
(val2 == o.val2 && ( val3 < o.val3 ) ) );
}
This seems efficient, but has several drawbacks:
These expressions get huge if the structs as a dozen or more members.
It is cumbersome to write and maintain if members change.
It needs to be done for every struct separately.
Is there a more maintainable way to compare structs like this?
You can use the builtin comparison that ships with <tuple> like this:
#include <tuple>
bool A::operator < (const A& rhs) const {
return std::tie(val1, val2, val3) < std::tie(rhs.val1, rhs.val2, rhs.val3);
}
This doesn't scale when more and more data members are added to the struct, but this might also be a hint that you could create intermediate structs that implement operator < and hence play well with the above implementation of a top-level operator <.
Let me add three additional comments on operator <.
Once you have operator <, clients will expect that all other comparison operators are provided, too. Before we have the three-way comparison in C++20, you can avoid unnecessary boilerplate code by e.g. using the Boost operator library:
#include <boost/operators.hpp>
struct A : private boost::totally_ordered<A> { /* ... */ };
which generates all operators based on operator < and operator == for you.
In your example, there is no need for the operator to be a member of A. You can make it a free function, which is preferable (see here for the rationale).
If there is no intrinsic ordering related to A and you just need operator < to store instances as keys in a std::map, consider providing a named predicate.
Great answer by lubgr.
One further refinement I perform is the creation of a member function as_tuple on any object which is to be ordered by its members:
#include <string>
#include <tuple>
#include <iostream>
struct A
{
float val1;
std::string val2;
int val3;
// provide easy conversion to tuple
auto as_tuple() const
{
return std::tie(val1, val2, val3);
}
};
Which often gives rise to thoughts of a general system of making objects and tuples interchangeable in terms of comparisons
template<class T> auto as_tuple(T&& l) -> decltype(l.as_tuple())
{
return l.as_tuple();
}
template<class...Ts>
auto as_tuple(std::tuple<Ts...> const& tup)
-> decltype(auto)
{
return tup;
}
template<class L, class R>
auto operator < (L const& l, R const& r)
-> decltype(as_tuple(l), void(), as_tuple(r), void(), bool())
{
return as_tuple(l) < as_tuple(r);
}
Which allows such code as:
int main()
{
auto a = A { 1.1, "foo", 0 };
auto b = A { 1.1, "foo", 1 };
auto test1 = a < b;
std::cout << test1 << std::endl;
auto test2 = a < std::make_tuple(1.1, "bar", 0);
std::cout << test2 << std::endl;
auto test3 = std::make_tuple(1.0, "bar", 0) < std::make_tuple(1.1, "bar", 0);
std::cout << test3 << std::endl;
auto test4 = a < std::make_tuple(2l, std::string("bar"), 0);
std::cout << test4 << std::endl;
}
example: http://coliru.stacked-crooked.com/a/ead750f3f65e3ee9

std::set iterator with same key and different comparators

While trying to solve a problem I started thinking about this - given a user-defined class and 2 comparators for it, lets say we have 2 sets std::set<user_class,comparator_inc> and std::set<user_class,comparator_dec> where the comparators sort by increasing and decreasing value on a value in the user_class(a simple int perhaps). Here's my code:
#include <iostream>
#include <set>
using std::cout;
using std::endl;
using std::set;
struct A
{
int val;
};
struct c_inc
{
bool operator()(const A& first,const A& second) const
{
return first.val > second.val;
}
};
struct c_dec
{
bool operator()(const A& first,const A& second) const
{
return first.val < second.val;
}
};
int main()
{
set<A,c_inc> s1;
set<A,c_dec> s2;
auto x = s1.insert({1});
cout << x.first->val << endl;
x = s2.insert({1});
x = s2.insert({0});
cout << x.first->val << endl;
}
My question is: Is it defined behavior to re-assign x to the output of insert into a set with same Key but different comparator? Is there a problem with this kind of use? Is it defined in the standard somewhere what it should be or is it implementation dependent?
Since the code compiles I think that the return type of insert in both cases is the same - is this assumption correct?
I think it's implementation dependent.
Conceptually the return type of s1.insert and s2.insert are different; especially they have different iterator types, i.e. std::set<A,c_inc>::iterator and std::set<A,c_dec>::iterator. And how the std::set::iterator's type is defined is implementation-defined.
[set.overview]/2
using iterator = implementation-defined; // see [container.requirements]
using const_iterator = implementation-defined; // see [container.requirements]
Technically speaking, you shouldn't rely on this.
Since the code compiles I think that the return type of insert in both
cases is the same - is this assumption correct?
No, it is not. Imagine this simple example:
template<class T>
struct set {
struct iterator { /*...*/ };
};
In this case set<int>::iterator is definitely different from set<double>::iterator.
The implementation is free to implement the iterator type as a free class though (since the iterator does not depend on the comparator), which seems to be the case in the major implementations, and is what's allowing your example.

c++ auto-comparator for structs

There is many primitive structs (several hundreds), that are used to transfer data between two components (for example a player and a server). There are no methods in them, just raw data.
The task is to write all requests and answers to be able to replay a player scenario without a server (we remember all question and all answers, that are pure functions).
So the task is put this structs in map without comparator. Now we use memcmp, it allows not to think about changes in this structs and it is compact, but there are too many problems with padding and etc.
Is it possible to get smth like getHashValue or any default comparator with metaprogramming in c++?
Conditions:
1) I do not want to create a comparator for each struct.
2) I want to have an error if a field was added or deleted if it breaks existing behavior and needs fix.
3) I don't want to change header files with struct definitions.
Example of a struct
struct A {
int a;
int b;
c c;
}
bool operator<(const A& a1, const A& a2)
{
if (a1.a != a2.a) return a1.a < a2.a;
if (a1.b != a2.b) return a1.b < a2.b;
if (a1.c != a2.c) return a1.c < a2.c;
return false;
}
I can consider other languages to implement this exact part (collect questions/answers), if it will not require to describe all this structs on that language again.
In C++17 you can pull this off if you are willing to (A) hard code how many elements are in each struct somewhere, and (B) write or generate code for each count of number of elements in the struct.
template<std::size_t N>
using size_k = std::integral_constant<std::size_t, N>;
template<class T>
auto auto_tie( size_k<0>, T&& t ) {
return std::tie();
}
template<class T>
auto auto_tie( size_k<1>, T&& t ) {
auto& [ x0 ] = std::forward<T>(t);
return std::tie( x0 );
}
template<class T>
auto auto_tie( size_k<2>, T&& t ) {
auto& [ x0, x1 ] = std::forward<T>(t);
return std::tie( x0, x1 );
}
// etc
now, in the namespace of the struct in question, write
struct foo {
int x;
};
struct bar {
int a, b;
};
size_k<1> elems( foo const& ) { return {}; }
size_k<2> elems( bar const& ) { return {}; }
an elems function that return the size_k counting how many elements.
Now in the namespace of the structs, write:
template<class T, class Size=decltype(elems(std::declval<T const&>()))>
bool operator<( T const& lhs, T const& rhs ) {
return auto_tie( Size{}, lhs ) < auto_tie( Size{}, rhs );
}
and you are done.
Test code:
foo f0{1}, f1{2};
bar b0{1,2}, b1{-7, -3};
std::cout << (f0<f1) << (f1<f0) << (f0<f0) << "\n";
std::cout << (b0<b1) << (b1<b0) << (b0<b0) << "\n";
Live example.
Getting further than this will require writing 3rd party tools or waiting for reflection extension to C++, maybe in C++20 or 23.
If you get elems wrong, I believe the structured bindings code in auto_tie should generate an error.
I suppose you could write your own compare operator based upon memcmp.
bool operator<(const A &lhs, const A &rhs) {
return memcmp(&lhs, &rhs, sizeof(A)) < 0;
}
Off course, writing these for each object might be a burden, so you could write a template for this. Though without some SFINAE it will cover too much types.
#include <type_traits>
#include <cstring>
template<typename T>
std::enable_if_t<std::is_pod_v<std::decay_t<T> //< Check if POD
&& !std::is_fundamental_v<std::decay_t<T>>>, //< Don't override for fundamental types like 'int'
bool>
operator<(const T &lhs, const T &rhs) {
return memcmp(&lhs, &rhs, sizeof(std::decay_t<T>)) < 0;
}
EDIT: Note that this technique requires you to zero-initialize the structs.
Looks like the best way to do it is to write a generator, that will generate .h and .cpp with bool operator< for all types in this header file. Then add this project as pre-build step to the main.
It doesn't look like a nice solution, but it allows to avoid hand-written code duplication and will support adding/removing new structs/fields. So I didn't find a better way.

Having trouble interpreting this C++ code

Background about code posted: PayRoll is the name of the class. personSalary is a double type variable, and personAge is an integer type variable. The code given is sorting a list by age or by the salary.
struct less_than_salary
{
inline bool operator() (const PayRoll& struct1, const PayRoll& struct2)
{
return (struct1.personSalary < struct2.personSalary);
}
};
struct less_than_age
{
inline bool operator() (const PayRoll& struct1, const PayRoll& struct2)
{
return (struct1.personAge < struct2.personAge);
}
};
I would like some help understanding this section of the given code. I've tried reading what struct is used for and from what I understand, it basically operates as a class and allows you to work with many types of variables at one time. If I'm wrong, what exactly is a struct used for in this context?
Also, I would appreciate it if someone explained what "inline bool operator()" was doing because I've never seen that before and I could not understand by reading the textbook.
Thank you for your help!
Both structs are implementations of a so-called "functor". Given usage like
PayRoll x;
PayRoll y
// initialise x and y
less_than_salary f;
if (f(x,y)) // this calls less_than_salary::operator()(x,y)
std::cout << "X has lower salary than Y\n";
else
std::cout << "X has higher salary than Y\n";
Also, given an array of PayRoll, it is possible to sort
PayRoll a[20];
// initialise elements of a
less_than_salary f;
std::sort(a, a+20, f); // will sort in order of ascending salary
std::sort(a, a+20, less_than_salary()); // does same as preceding two lines
Standard containers (std::vector<PayRoll>, etc) can also be used.
less_than_age allows doing essentially the same thing, but using age rather tnan salary as a sort criterion.
There is no function overloading as such here. Both struct types provide an operator(), but that is not overloading.
A struct like this can be used in the STL function sort, which is avaliable in data structures such as std::list.
#include <list>
#include <iostream>
int main() {
std::list<int> example = {9,8,7,6,5,4,3,2,1};
struct {
bool operator()( const int lhs, const int rhs) {
return lhs < rhs;
}
} compare_list;
example.sort(compare_list);
for(auto &i : example) {
std::cout<<i<<" ";
}
}
Output:
1 2 3 4 5 6 7 8 9
To understand how this works, consider the following:
//..
testS(compare_list);
//..
template <typename T>
void testS(T t) {
std::cout<< t(4,5) << "\n";
std::cout<< t(5,4) << "\n";
}
The output would be
1
0