名稱空間
變體
操作

std::forward_like

來自 cppreference.com
< 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 引用
(函式模板) [編輯]