N4175: Default comparisons -- Bjarne Stroustrup

A new WG21 paper is available. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N4175

Date: 2014-10-11

Default comparisons

by Bjarne Stroustrup

Excerpt:

Defining comparison operators (==, !=, <, <=, >, and >=) for simple classes is tedious, repetitive, slightly error-prone, and easily automated. I propose to (implicitly) supply default versions of these operations, if needed. The semantics of = is equality of every member. The meaning of < is a lexicographical order of elements. If the simple defaults are unsuitable for a class, a programmer can, as ever, define more suitable ones or suppress the defaults.

Add a Comment

Comments are closed.

Comments (2)

0 0

Marius Bancila said on Oct 13, 2014 04:06 AM:

"If a class has a pointer member, == and != are not generated."

To me this is not intuitive and furthermore it is inconsistent with existing behavior for other special functions default implementation. I believe it should threat pointers just like operator= does and do a "shallow comparison", i.e. compare the value of the pointer and not the value of the pointed object. But these operators should be default generated (in an explicit implementation is not provided) in all cases.
0 0

Malcolm Parsons said on Oct 28, 2014 11:06 AM:

Top of page 4: "or we could use !(x==y)"

I think that should say "or we could use !(x!=y)"