C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Should postfix increment and decrement operators be automatically obtainable from their prefix versions?

From: Jan Schultke <janschultke_at_[hidden]>
Date: Wed, 24 Jan 2024 19:41:29 +0100
> Also, the implementation that calls operator+ is extremely unlikely to ever be preferable to the one that uses prefix operator++.

This isn't the strongest argument, but operator+ is preferable if
operator++(void) cannot be any better, e.g. if you have a random
access operator and operator++(void) delegates to operator+ anyway.

In that case, you save yourself one unnecessary instantiation of
operator++(void) by making operator++(int) go directly to operator+.
This is similar to saving one useless instantiation of operator> by
delegating all comparison operators to operator< directly.

Personally, I think there should be more flexibility than just "=
default" or having the compiler generate it implicitly for you.
Perhaps a syntax such as:

> operator++(int) = operator++;

This is barely longer than "= default" and lets you manually pick what
delegation you want. This would be particularly useful for e.g. Matrix
multiplication, where operator* delegates to operator*=, despite the
opposite delegation being the norm.

Received on 2024-01-24 18:41:41