std::decay
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <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 に減衰型変換を適用した結果
|
ヘルパー型
<tbody> </tbody> 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 を合わせたもの (クラステンプレート) |
| 暗黙の変換 | 配列からポインタ、関数からポインタ、左辺値から右辺値への変換 |