std::unwrap_reference, std::unwrap_ref_decay
来自cppreference.com
< cpp | utility | functional
在标头 <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&
。
如果程序添加了此页面上描述的任何模板的特化,那么行为未定义。
目录 |
[编辑]嵌套类型
类型 | 定义 |
type | (1) |
[编辑]辅助类型
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<constint>, constint>); 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<constint>, int>); static_assert(std::is_same_v<std::unwrap_ref_decay_t<constint&>, 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&// 对于 reference_wrapper<U> 将 T&& 转换为 U& decltype(auto) r = std::unwrap_reference_t<T>(z);std::cout<<"r:"<< r <<'\n'; r =0;// OK,r 具有引用类型}; int x =1; reset(x);assert(x ==0); int y =2; reset(std::ref(y));assert(y ==0);}}
输出:
r:1 r:2
[编辑]参阅
(C++11) | 可复制构造(CopyConstructible) 且可复制赋值(CopyAssignable) 的引用包装器 (类模板) |
创建一个 pair 对象,其类型根据各实参类型确定 (函数模板) | |
(C++11) | 创建一个 tuple 对象,其类型根据各实参类型定义 (函数模板) |