Namespaces
Variants

std:: unwrap_reference, std:: unwrap_ref_decay

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Reference wrappers
(C++11) (C++11)
unwrap_reference unwrap_ref_decay
(C++20) (C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until 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) 如果 T std::reference_wrapper 的特化,则解包它;否则, T 保持不变。
2) 若衰变后的 T std::reference_wrapper 的特化,则展开它;否则, T 保持衰变后的类型。

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

目录

嵌套类型

类型 定义
type

(1) T std:: 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_pair std::make_tuple 相同的转换。

功能测试 标准 功能
__cpp_lib_unwrap_ref 201811L (C++20) std::unwrap_ref_decay std::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; // 错误:如果 T 是 reference_wrapper<> 则无法工作
            // 将 T&& 转换为普通类型的 T&
            // 将 T&& 转换为 reference_wrapper<U> 的 U&
            decltype(auto) r = std::unwrap_reference_t<T>(z);
            std::cout << "r: " << r << '\n';
            r = 0; // 正确,r 具有引用类型
        };
        int x = 1;
        reset(x);
        assert(x == 0);
        int y = 2;
        reset(std::ref(y));
        assert(y == 0);
    }
}

输出:

r: 1
r: 2

参见

CopyConstructible CopyAssignable 引用包装器
(类模板)
创建类型由实参类型决定的 pair 对象
(函数模板)
(C++11)
创建由实参类型定义的 tuple 对象
(函数模板)