C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 1 Dec 2024 00:34:33 -0500
FWIW, this area was extensively explored quite a few years ago by David
Stone in P1046 "Automatically generate more operators"
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1046r2.html>
(2020). But in 2023 he abandoned the wider proposal in favor of a
drastically different P3039 "Automatically generate operator->"
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p3039r0.html>.
Neither paper has yet made it as far as having any proposed wording.

One of the drastic changes in P3039 was the idea that maybe instead of
making
  auto operator->() const = default;
define a new member function definition, we should simply add a rewrite
rule just like the existing (C++20) rewrite rule for (a < b). We could say
that (a -> b) will perform lookup for both (a.operator->) *and*
(a.operator*) in exactly the same way that (a < b) performs lookup for both
(a.operator<) *and* (a.operator<=>). (And add similar rules for deciding
which candidate is the best match.)

This would also influence your proposal about (a++): Should you actually
make
  auto operator++(int) = default;
define a new member function definition, or should you simply add a rewrite
rule that makes (a++) perform lookup for both variants of operator++ and
fall back to the prefix version (plus the copy constructor) if the postfix
version isn't available? Your motivation touches on that question here:

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.


You *can* write `operator!=(...) = default`, but no C++20 programmer ever
*would* write it, because we have a rewrite rule that handles that
transparently, without the programmer's intervention. So, it seems like it
would make sense to handle postfix `++` in the same way, right?

my $.02,
–Arthur


On Sun, Dec 1, 2024 at 12:10 AM Magnus Fromreide via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> 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
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-12-01 05:34:51