C++ Logo

std-discussion

Advanced search

Re: std::get on const rvalue

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Thu, 8 Dec 2022 05:44:42 +0100
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.


My 2 c,
-- 
Giuseppe D'Angelo

Received on 2022-12-08 04:44:46