C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Defect report for std::apply (or apply_perfectly)

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sun, 10 Mar 2024 12:01:39 +0000
On Sun, 10 Mar 2024 at 10:38, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
>
> std::apply works as follows:
>
> return invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...);
>
> If one of the types in the tuple is an Rvalue reference, it becomes an Lvalue.

Only if the tuple itself is an lvalue. If the tuple is an rvalue, then
std::get returns an rvalue.

> I can't imagine that this was intentional.

It was.

What you're observing is the behaviour of std::get, not a problem in std::apply.

> I have written my own function called 'apply_perfectly' which takes the return value from 'get' and uses 'static_cast' to cast it to the type of the tuple element, thereby achieving perfect forwarding.
>
> So my question is as follows, which should I do?
>
> A) Write a defect report for std::apply
> B) Write a new paper for std::apply_perfectly

Forward your tuple<T&&> as an rvalue not an lvalue.

Why should std::apply work differently from everything else that uses std::get?

Received on 2024-03-10 12:02:57