In the following code example, because I compare two std::map
objects, I am compelled to implement MyId::operator==()
.
MyId
is a simple class: basically a wrapper over an array. This made me wonder why an equality operator is mandated: why aren't we given a default bitwise comparator? After all, we are given a default assignment operator, which does a bitwise memberwise copy between objects.
I understand the difference between deep and shallow copies and the need for assignment and equality operators. My question is why is there not a default "bitwise equality check" when there is a default assignment?
#include <iostream>
#include <map>
#include <cstring>
typedef int myid[ 3 ];
struct MyId
{
myid id_;
bool operator<( const struct MyId& other ) const { return this < &other; }
bool operator==( const struct MyId& other ) const
{
return ( 0 == std::memcmp( id_, other.id_, sizeof( myid ) ));
}
};
int main( int argc, char* argv[] )
{
std::map< MyId, int> map;
MyId id = { 1, 2, 3 };
map[ id ] = 5;
std::map< MyId, int> map2;
map2[ id ] = 5;
// This forces implementation of MyId::operator==():
std::cout << std::boolalpha <<( map == map2 ) << std::endl;
MyId i1 = { 4, 5, 6 };
MyId i2 = { 7, 8, 9 };
// No required implementation of MyId::operator=():
i1 = i2;
return 0;
}
Update: Publicly corrected my brain-fart RE: bitwise vs. memberwise default assignment operator.