std::decay
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct decay; |
(C++11 起) | |
執行與透過值傳遞函式引數時執行的型別轉換等效的轉換。形式上:
- 若
T
是“U
陣列”或其引用,則成員 typedeftype
為U*
。
- 否則,若
T
是函式型別F
或其引用,則成員 typedeftype
為 std::add_pointer<F>::type。
- 否則,成員 typedef
type
為 std::remove_cv<std::remove_reference<T>::type>::type。
若程式為 std::decay
新增特化,則行為未定義。
目錄 |
[編輯] 成員型別
名稱 | 定義 |
型別
|
對 T 應用衰變型別轉換後的結果 |
[編輯] 輔助型別
template< class T > using decay_t = typename decay<T>::type; |
(C++14 起) | |
[編輯] 可能實現
template<class T> struct decay { private: typedef typename std::remove_reference<T>::type U; public: typedef typename std::conditional< std::is_array<U>::value, typename std::add_pointer<typename std::remove_extent<U>::type>::type, typename std::conditional< std::is_function<U>::value, typename std::add_pointer<U>::type, typename std::remove_cv<U>::type >::type >::type type; }; |
[編輯] 示例
執行此程式碼
#include <type_traits> template<typename T, typename U> constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>; int main() { static_assert ( is_decay_equ<int, int> && ! is_decay_equ<int, float> && is_decay_equ<int&, int> && is_decay_equ<int&&, int> && is_decay_equ<const int&, int> && is_decay_equ<int[2], int*> && ! is_decay_equ<int[4][2], int*> && ! is_decay_equ<int[4][2], int**> && is_decay_equ<int[4][2], int(*)[2]> && is_decay_equ<int(int), int(*)(int)> ); }
[編輯] 參閱
(C++20) |
結合了 std::remove_cv 和 std::remove_reference (類模板) |
隱式轉換 | 陣列到指標,函式到指標,左值到右值轉換 |