名稱空間
變體
操作

std::unwrap_reference, std::unwrap_ref_decay

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
引用包裝器
(C++11)(C++11)
unwrap_referenceunwrap_ref_decay
(C++20)(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <type_traits>
定義於標頭檔案 <functional>
template< class T >
struct unwrap_reference;
(1) (C++20 起)
template< class T >
struct unwrap_ref_decay;
(2) (C++20 起)

解包任何 std::reference_wrapper:將 std::reference_wrapper<U> 更改為 U&

1) 如果 Tstd::reference_wrapper 的特化,則解包它;否則,T 保持不變。
2) 如果衰減後的 Tstd::reference_wrapper 的特化,則解包它;否則,T 被衰減。

如果程式為此頁上描述的任何模板新增特化,則行為未定義。

目錄

[編輯] 巢狀型別

型別 定義
型別

(1) 如果 Tstd::reference_wrapper<U>,則為 U&;否則為 T
(2) 如果 std::decay_t<T>std::reference_wrapper<U>,則為 U&;否則為 std::decay_t<T>

[編輯] 輔助型別

template<class T>
using unwrap_reference_t = unwrap_reference<T>::type;
(1) (C++20 起)
template<class T>
using unwrap_ref_decay_t = unwrap_ref_decay<T>::type;
(2) (C++20 起)

[編輯] 可能的實現

template<class T>
struct unwrap_reference { using type = T; };
template<class U>
struct unwrap_reference<std::reference_wrapper<U>> { using type = U&; };
 
template<class T>
struct unwrap_ref_decay : std::unwrap_reference<std::decay_t<T>> {};

[編輯] 注意

std::unwrap_ref_decay 執行與 std::make_pairstd::make_tuple 所使用的轉換相同。

特性測試 標準 特性
__cpp_lib_unwrap_ref 201811L (C++20) std::unwrap_ref_decaystd::unwrap_reference

[編輯] 示例

#include <cassert>
#include <functional>
#include <iostream>
#include <type_traits>
 
int main()
{
    static_assert(std::is_same_v<std::unwrap_reference_t<int>, int>);
    static_assert(std::is_same_v<std::unwrap_reference_t<const int>, const int>);
    static_assert(std::is_same_v<std::unwrap_reference_t<int&>, int&>);
    static_assert(std::is_same_v<std::unwrap_reference_t<int&&>, int&&>);
    static_assert(std::is_same_v<std::unwrap_reference_t<int*>, int*>);
 
    {
        using T = std::reference_wrapper<int>;
        using X = std::unwrap_reference_t<T>;
        static_assert(std::is_same_v<X, int&>);
    }
    {
        using T = std::reference_wrapper<int&>;
        using X = std::unwrap_reference_t<T>;
        static_assert(std::is_same_v<X, int&>);
    }
 
    static_assert(std::is_same_v<std::unwrap_ref_decay_t<int>, int>);
    static_assert(std::is_same_v<std::unwrap_ref_decay_t<const int>, int>);
    static_assert(std::is_same_v<std::unwrap_ref_decay_t<const int&>, int>);
 
    {
        using T = std::reference_wrapper<int&&>;
        using X = std::unwrap_ref_decay_t<T>;
        static_assert(std::is_same_v<X, int&>);
    }
 
    {
        auto reset = []<typename T>(T&& z)
        {
        //  x = 0; // Error: does not work if T is reference_wrapper<>
            // converts T&& into T& for ordinary types
            // converts T&& into U& for reference_wrapper<U>
            decltype(auto) r = std::unwrap_reference_t<T>(z);
            std::cout << "r: " << r << '\n';
            r = 0; // OK, r has reference type
        };
 
        int x = 1;
        reset(x);
        assert(x == 0);
 
        int y = 2;
        reset(std::ref(y));
        assert(y == 0);
    }
}

輸出

r: 1
r: 2

[編輯] 另見

CopyConstructibleCopyAssignable 引用包裝器
(類模板) [編輯]
建立型別由引數型別確定的 pair 物件
(函式模板) [編輯]
建立一個由引數型別定義的 tuple 物件
(函式模板) [編輯]