C++ Logo

std-proposals

Advanced search

Re: [std-proposals] better container::append_range?

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Mon, 17 Nov 2025 06:45:01 +0000
On Sun, 16 Nov 2025, 15:47 Hewill Kang via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> My habitual mental model is that functions returning a certain value are
> always better than returning void because they carry more information.
>

Returning *this certainly returns more information then void, but it's
information that you already have at the call site. It's not _new_
information, and it's not even information that's difficult to obtain at
the call site.

On the other hand, returning an iterator from std::fill_n is valuable
information that isn't known at the call site (lwg 839). Similarly,
returning an iterator from std::rotate avoids an O(n) operation at the call
site to re-calculate that information (lwg 488).

Returning a reference from c.emplace_back(...) is information which is
_available_ at the call site, but it requires saying c.back() which
sometimes needs to do more pointer arithmetic to find the last element.
Since emplace_back already did that arithmetic, it avoids repeating the
calculation to find the address of the last element.

But in c.append_range(r) if you return c then that is something which the
caller already knows.



But after your explanation, I learned that sometimes returning void might
> be more meaningful.
>


I don't think it had more meaning, but it maybe does less work and maybe
saves a few instructions. If those instructions are not doing useful work,
they're a waste.

Received on 2025-11-17 06:45:18