std::forward_like

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

���回拥有与 T&& 相似属性的到 x 的引用。

返回类型确定如下:

  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_conststd::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 使用合并 模型。

功能特性测试 标准 功能特性
__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 << "常量左值\n";
            else
                std::cout << "可变左值\n";
        }
        else
        {
            if constexpr (std::is_const_v<UnrefSelfType>)
                std::cout << "常量右值\n";
            else
                std::cout << "可变右值\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> 始终解引用到非 const 左值。
    }
};
 
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';
}

输出:

可变左值
可变左值
可变左值
 
常量左值
常量左值
常量左值
 
可变右值
可变右值
可变右值
 
常量右值
常量右值
常量右值

[编辑] 参阅

(C++11)
转换实参为亡值
(函数模板) [编辑]
(C++11)
转发一个函数实参,并使用模板实参保留它的值类别
(函数模板) [编辑]
(C++17)
获得到其实参的 const 引用
(函数模板) [编辑]