C++ Logo

std-discussion

Advanced search

Re: std::get on const rvalue

From: Yongwei Wu <wuyongwei_at_[hidden]>
Date: Fri, 9 Dec 2022 12:13:52 +0800
On Thu, 8 Dec 2022 at 12:44, Giuseppe D'Angelo via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> Hello,
>
> Il 08/12/22 02:58, Yongwei Wu via Std-Discussion ha scritto:
> > Specifically, if I want to implement a tuple-like object, is it
> > necessary to implement std::get on const rvalue reference?
>
>
> If the get() for your tuple-like is accessing a member, you can simplify
> the implementation considerably:
>
> > template <std::size_t I, typename T,
> > std::enable_if_t<(I < 123), bool> = true,
> > std::enable_if_t<std::is_same_v<std::decay_t<T>, A>, bool> = true>
> > decltype(auto) get(T &&t)
> > {
> > if constexpr (I == 0)
> > return (std::forward<T>(t).m);
> > // else if constexpr ...
> > }
>
> (where XXX is tuple_size<A> or similar), which will also yield const
> rvalue references when called on a const rvalue A. The same
> implementation is very convenient as a non-static member function of A,
> using "deduced this" (rather than constraining the argument's type).
>
> (If it's not a member, then this is likely a matter for C++23's
> forward_like and its related discussions on how to propagate cv-refs
> properly.)
>
> Const rvalue references are maybe useless in practice, but they're part
> of the language. The trick is using facilities that don't require us to
> spell them out in generic code.

Thank you. You are right, thinking and implementing such functions in
terms of forwarding should be the way out.

Received on 2022-12-09 04:14:04