命名空间
变体
操作

std::unwrap_reference, std::unwrap_ref_decay

来自 cppreference.cn
< cpp‎ | utility‎ | functional
 
 
 
函数对象
函数调用
(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 会衰退。

如果程序为此页面上描述的任何模板添加特化,则行为未定义。

目录

[编辑] 嵌套类型

类型 定义
type

(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 使用的相同的转换。

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