Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading operator == for pods

I am working on some low level code with high level interfaces and felt need for comparisons operator for unit testing for plain old data types(like FILETIME struct) but since C++ doesn't even provide memberwise comparisons, so I wrote this:

template <typename Type>
std::enable_if_t<std::is_pod<Type>::value, bool> operator==(const Type& a,
                                                            const Type& b) {
  return std::memcmp(&a, &b, sizeof(Type)) == 0;
}

So my question is, is this a good way or there are some hidden demons which will give me trouble later down the development cycle but it's kinda working for now.

like image 451
jaganantharjun Avatar asked Jun 03 '18 13:06

jaganantharjun


1 Answers

Is C++14 available? If so, consider PFR library, which makes structures into tuples

like image 148
Alex Guteniev Avatar answered Sep 21 '22 13:09

Alex Guteniev