C++ Logo

std-proposals

Advanced search

[std-proposals] Defaulted operator++(int) and operator--(int)

From: Magnus Fromreide <magfr_at_[hidden]>
Date: Sun, 1 Dec 2024 06:10:01 +0100
Hello!

Would it be reasonable to support defaulted operator++/--(int)?
We have all written a million of them

T operator++(int)
{
        T rv = *this;
        ++*this;
        return rv;
}

and they are almost always the same. Would it not be nice to be able to just
write

T operator++(int) = default;

and never have to bother with it again, just like you can write

bool operator!=(const T&, const T&) = default;

and get the expected result.

The preconditions for defaulted operator++/-- is obviously that

a) operator++/--() exists
b) *this is assignable to T


I agree that this is a very small addition but it saves one from writing
some boilerplate.

I am also convinced that someone could express this as a very elegant piece
of reflection and generation but that someone is not me at this point.


Finally, I do not propose that operator++(int) should be defaulted whenever
operator++() exists as that would be a change to current behaviour and
someone somewhere sureley depends on

++theirtype; behaving exactly like theirtype++;

so that ship sailed long ago.

/MF

Received on 2024-12-01 05:10:08