std::decay
提供: cppreference.com
ヘッダ <type_traits> で定義
|
||
template< class T > struct decay; |
(C++11以上) | |
型 T
に左辺値から右辺値、配列からポインタ、および関数からポインタへの暗黙の変換を適用し、 cv 修飾子を除去し、結果の型をメンバ型 type
として定義します。 形式的には、
-
T
が「U
の配列」型または「U
の配列への参照」型であれば、メンバ型type
は U* です。
- そうでなく、
T
が関数型F
またはその参照であれば、メンバ型type
は std::add_pointer<F>::type です。
- そうでなければ、メンバ型
type
は std::remove_cv<std::remove_reference<T>::type>::type です。
これらの変換は値渡しするときにすべての関数の引数に適用される型変換をモデル化します。
目次 |
[編集] メンバ型
名前 | 定義 |
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::remove_extent<U>::type*, typename std::conditional< std::is_function<U>::value, typename std::add_pointer<U>::type, typename std::remove_cv<U>::type >::type >::type type; }; |
[編集] 例
Run this code
#include <iostream> #include <type_traits> template <typename T, typename U> struct decay_equiv : std::is_same<typename std::decay<T>::type, U>::type {}; int main() { std::cout << std::boolalpha << decay_equiv<int, int>::value << '\n' << decay_equiv<int&, int>::value << '\n' << decay_equiv<int&&, int>::value << '\n' << decay_equiv<const int&, int>::value << '\n' << decay_equiv<int[2], int*>::value << '\n' << decay_equiv<int(int), int(*)(int)>::value << '\n'; }
出力:
true true true true true true
[編集] 関連項目
(C++20) |
std::remove_cv と std::remove_reference を合わせたもの (クラステンプレート) |
暗黙の変換 | 配列からポインタ、関数からポインタ、左辺値から右辺値への変換 |