命名空间
变体
操作

std::forward_like

来自 cppreference.cn
< cpp‎ | utility
 
 
 
定义于头文件 <utility>
template< class T, class U >
constexpr auto&& forward_like( U&& x ) noexcept;
(自 C++23 起)

返回对 x 的引用,该引用具有类似于 T&& 的属性。

返回类型确定如下

  1. std::remove_reference_t<T> 是 const 限定类型,则返回类型的被引用类型为 const std::remove_reference_t<U>。否则,被引用类型为 std::remove_reference_t<U>
  2. T&& 是左值引用类型,则返回类型也为左值引用类型。否则,返回类型为右值引用类型。

T 不是可引用类型,则程序为非良构。

内容

[编辑] 参数

x - 需要像类型 T 一样转发的值

[编辑] 返回值

x 的引用,其类型如上所述确定。

[编辑] 注解

std::forwardstd::movestd::as_const 类似,std::forward_like 是一种类型转换,仅影响表达式的值类别,或可能添加 const 限定符。

m 是实际成员且因此 o.m 是有效表达式时,这通常在 C++20 代码中拼写为 std::forward<decltype(o)>(o).m

这导致三种可能的模型,称为合并元组语言

  • 合并:合并 const 限定符,并采用 Owner 的值类别。
  • 元组std::get<0>(Owner) 所做的,假设 Ownerstd::tuple<Member>
  • 语言std::forward<decltype(Owner)>(o).m 所做的。

std::forward_like 主要针对适应“远端”对象的情况。元组语言场景都不能为主要用例做正确的事情,因此 std::forward_like 使用合并模型。

特性测试 Std 特性
__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 引用
(函数模板) [编辑]