命名空间
变体
操作

std::forward_like

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

返回一个对 x 的引用,其特性与 T&& 相似。

返回类型确定如下:

  1. 如果 std::remove_reference_t<T> 是一个 const-qualified 类型,那么返回类型的引用类型是 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

这导致了三种可能的模型,称为 mergetuplelanguage

  • merge:合并 const 限定符,并采用 Owner 的值类别。
  • tuplestd::get<0>(Owner) 所做的,假设 Owner 是一个 std::tuple<Member>
  • languagestd::forward<decltype(Owner)>(o).m 所做的。

std::forward_like 主要针对的场景是适配“远”对象。对于这个主要用例,tuplelanguage 场景都无法正确处理,因此 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)
将参数转换为亡值
(函数模板) [编辑]
(C++11)
转发函数参数并使用类型模板参数来保留其值类别
(函数模板) [编辑]
(C++17)
获取其参数的 const 引用
(函数模板) [编辑]