std::unwrap_reference, std::unwrap_ref_decay
來自 cppreference.com
定義於標頭檔案 <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) 如果 |
[編輯] 輔助型別
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; // 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
[編輯] 另見
(C++11) |
CopyConstructible 和 CopyAssignable 引用包裝器 (類模板) |
建立型別由引數型別確定的 pair 物件(函式模板) | |
(C++11) |
建立一個由引數型別定義的 tuple 物件(函式模板) |