C++ Logo

std-proposals

Advanced search

Re: Operator for reference casting

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 17 Jan 2021 12:20:55 -0500
On Sun, Jan 17, 2021 at 4:27 AM Drew Gross via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Some examples of when you don't want to forward a forwarding reference, might be easy to create bugs if it forwarded automatically:
>
> 1) A generic logging wrapper function
> ```
> auto LoggedCall(auto >&& f, auto >&& v) {
> LogValue(v);
> return f(v); // Oops, v was moved from! (Maybe, depending on signature of LogValue)
> }
> ```

Why would a logging function move from its parameter? It should take
it as a `const&`.

> 2) A generic concatenate function
> ```
> auto Concatenate(auto >&& c1, auto >&& c2) {
> std::vector<decltype(c1)::value_type> result;
> using std::size;
> result.reserve(size(c1) + size(c2)); // Oops! c1 and c2 were moved from (OK only if size's signature is weird for c1/c2's type, but you get the point)
> // ... add c1 and c2 to result ...
> return result;
> }
> ```

Again, why would `size` move from its parameter?

Received on 2021-01-17 11:21:18