std::forward_like
From cppreference.com
| ヘッダ <utility> で定義 |
||
| template< class T, class U > constexpr auto&& forward_like( U&& x ) noexcept; |
(C++23から) | |
T&& と同様の特性を持つ x への参照を返します。
戻り値の型は以下のように決定されます。
- std::remove_reference_t<T> が const 修飾型である場合、戻り値の型の参照型は const std::remove_reference_t<U> となります。それ以外の場合、参照型は std::remove_reference_t<U> となります。
T&&が左辺値参照型である場合、戻り値の型も左辺値参照型となります。それ以外の場合、戻り値の型は右辺値参照型となります。
T が 参照可能な型 でない場合、プログラムは不正です。
目次 |
[編集] パラメーター
| x | - | 型 T のように転送される必要がある値 |
[編集] 戻り値
上記のように決定された型の x への参照。
[編集] 備考
std::forward, std::move, std::as_const と同様に、std::forward_like は、式の 値カテゴリ にのみ影響を与えたり、場合によっては const-qualification を追加したりする型キャストです。
m が実際のメンバーであり、したがって o.m が有効な式である場合、C++20 コードでは通常 std::forward<decltype(o)>(o).m と記述されます。
これにより、merge、tuple、language と呼ばれる3つのモデルが考えられます。
- merge: const 修飾子をマージし、
Ownerの値カテゴリを採用します。 - tuple:
Ownerが std::tuple<Member> であると仮定した場合の std::get<0>(Owner) が行うこと。 - language: std::forward<decltype(Owner)>(o).m が行うこと。
std::forward_like が対象とする主なシナリオは、「遠い」オブジェクトを適応させることです。tuple シナリオも language シナリオも、その主なユースケースに対して正しいことをしないため、std::forward_like には merge モデルが使用されます。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_forward_like |
202207L |
(C++23) | std::forward_like
|
[編集] 可能な実装
template<class T, class U> constexpr auto&& forward_like(U&& x) noexcept { constexpr bool is_adding_const = std::is_const_v<std::remove_reference_t<T>>; if constexpr (std::is_lvalue_reference_v<T&&>) { if constexpr (is_adding_const) return std::as_const(x); else return static_cast<U&>(x); } else { if constexpr (is_adding_const) return std::move(std::as_const(x)); else return std::move(x); } } |
[編集] 例
このコードを実行
#include <cstddef> #include <iostream> #include <memory> #include <optional> #include <type_traits> #include <utility> #include <vector> struct TypeTeller { void operator()(this auto&& self) { using SelfType = decltype(self); using UnrefSelfType = std::remove_reference_t<SelfType>; if constexpr (std::is_lvalue_reference_v<SelfType>) { if constexpr (std::is_const_v<UnrefSelfType>) std::cout << "const lvalue\n"; else std::cout << "mutable lvalue\n"; } else { if constexpr (std::is_const_v<UnrefSelfType>) std::cout << "const rvalue\n"; else std::cout << "mutable rvalue\n"; } } }; struct FarStates { std::unique_ptr<TypeTeller> ptr; std::optional<TypeTeller> opt; std::vector<TypeTeller> container; auto&& from_opt(this auto&& self) { return std::forward_like<decltype(self)>(self.opt.value()); // It is OK to use std::forward<decltype(self)>(self).opt.value(), // because std::optional provides suitable accessors. } auto&& operator[](this auto&& self, std::size_t i) { return std::forward_like<decltype(self)>(self.container.at(i)); // It is not so good to use std::forward<decltype(self)>(self)[i], because // containers do not provide rvalue subscript access, although they could. } auto&& from_ptr(this auto&& self) { if (!self.ptr) throw std::bad_optional_access{}; return std::forward_like<decltype(self)>(*self.ptr); // It is not good to use *std::forward<decltype(self)>(self).ptr, because // std::unique_ptr<TypeTeller> always dereferences to a non-const lvalue. } }; int main() { FarStates my_state { .ptr{std::make_unique<TypeTeller>()}, .opt{std::in_place, TypeTeller{}}, .container{std::vector<TypeTeller>(1)}, }; my_state.from_ptr()(); my_state.from_opt()(); my_state[0](); std::cout << '\n'; std::as_const(my_state).from_ptr()(); std::as_const(my_state).from_opt()(); std::as_const(my_state)[0](); std::cout << '\n'; std::move(my_state).from_ptr()(); std::move(my_state).from_opt()(); std::move(my_state)[0](); std::cout << '\n'; std::move(std::as_const(my_state)).from_ptr()(); std::move(std::as_const(my_state)).from_opt()(); std::move(std::as_const(my_state))[0](); std::cout << '\n'; }
出力
mutable lvalue mutable lvalue mutable lvalue const lvalue const lvalue const lvalue mutable rvalue mutable rvalue mutable rvalue const rvalue const rvalue const rvalue
[編集] 関連項目
| (C++11) |
引数をxvalueに変換する (関数テンプレート) |
| (C++11) |
関数引数を転送し、型テンプレート引数を使用してその値カテゴリを維持する (関数テンプレート) |
| (C++17) |
引数への const 参照を取得する (関数テンプレート) |