std:: forward_like
|
定义于头文件
<utility>
|
||
|
template
<
class
T,
class
U
>
constexpr auto && forward_like ( U && x ) noexcept ; |
(C++23 起) | |
返回对
x
的引用,其特性类似于
T&&
。
返回类型的确定方式如下:
- 如果 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 限定的类型转换。
当
m
是实际成员且
o.
m
为有效表达式时,在 C++20 代码中通常写作
std::
forward
<
decltype
(
o
)
>
(
o
)
.
m
。
这导致了三种可能的模型,分别称为 merge 、 tuple 和 language 。
-
merge
: 合并
const
限定符,并采用
Owner的值类别。 -
tuple
: 即
std
::
get
<
0
>
(
Owner
)
的操作,假定
Owner是 std:: tuple < Member > 。 - language : 即 std:: forward < decltype ( Owner ) > ( o ) . m 的操作。
std::forward_like
主要处理的场景是适配“远端”对象。无论是
元组
模型还是
语言
模型都无法正确处理这一核心用例,因此
std::forward_like
采用了
合并
模型。
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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()); // 使用 std::forward<decltype(self)>(self).opt.value() 是可行的, // 因为 std::optional 提供了适当的访问器。 } auto&& operator[](this auto&& self, std::size_t i) { return std::forward_like<decltype(self)>(self.container.at(i)); // 使用 std::forward<decltype(self)>(self)[i] 不太理想,因为 // 容器不提供右值下标访问,尽管它们本可以支持。 } auto&& from_ptr(this auto&& self) { if (!self.ptr) throw std::bad_optional_access{}; return std::forward_like<decltype(self)>(*self.ptr); // 使用 *std::forward<decltype(self)>(self).ptr 不好,因为 // std::unique_ptr<TypeTeller> 解引用始终返回非常量左值。 } }; 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()(
参阅
|
(C++11)
|
将参数转换为右值引用
(函数模板) |
|
(C++11)
|
转发函数参数,并使用模板类型参数保持其值类别
(函数模板) |
|
(C++17)
|
获取指向其参数的
const
引用
(函数模板) |