Why don’t C++ compilers define operator== and operator!=?

The argument that if the compiler can provide a default copy constructor, it should be able to provide a similar default operator==() makes a certain amount of sense. I think that the reason for the decision not to provide a compiler-generated default for this operator can be guessed by what Stroustrup said about the default copy constructor in “The Design and Evolution of C++” (Section 11.4.1 – Control of Copying):

I personally consider it unfortunate
that copy operations are defined by
default and I prohibit copying of
objects of many of my classes.
However, C++ inherited its default
assignment and copy constructors from
C, and they are frequently used.

So instead of “why doesn’t C++ have a default operator==()?”, the question should have been “why does C++ have a default assignment and copy constructor?”, with the answer being those items were included reluctantly by Stroustrup for backwards compatibility with C (probably the cause of most of C++’s warts, but also probably the primary reason for C++’s popularity).

For my own purposes, in my IDE the snippet I use for new classes contains declarations for a private assignment operator and copy constructor so that when I gen up a new class I get no default assignment and copy operations – I have to explicitly remove the declaration of those operations from the private: section if I want the compiler to be able to generate them for me.

Leave a Comment