名前空間
変種
操作

std::decay

提供: cppreference.com
< cpp‎ | types
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20未満)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
定数評価文脈
サポートされている操作
関係と性質の問い合わせ
型変更
(C++11)(C++11)(C++11)
型変換
decay
(C++11)
(C++11)
(C++17)
(C++11)(C++20未満)(C++17)
 
ヘッダ <type_traits> で定義
template< class T >
struct decay;
(C++11以上)

T に左辺値から右辺値、配列からポインタ、および関数からポインタへの暗黙の変換を適用し、 cv 修飾子を除去し、結果の型をメンバ型 type として定義します。 形式的には、

  • T が「U の配列」型または「U の配列への参照」型であれば、メンバ型 typeU* です。
  • そうでなく、 T が関数型 F またはその参照であれば、メンバ型 typestd::add_pointer<F>::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;
};

[編集]

#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

[編集] 関連項目

std::remove_cvstd::remove_reference を合わせたもの
(クラステンプレート) [edit]
暗黙の変換 配列からポインタ、関数からポインタ、左辺値から右辺値への変換