std::decay
From 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 の特殊化を追加した場合、その動作は未定義です。
目次 |
[編集] メンバ型
| 名前 | 定義 |
type
|
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 を組み合わせる (クラステンプレート) |
| 暗黙の変換 | 配列からポインタへの変換、関数からポインタへの変換、lvalueからrvalueへの変換 |