名前空間
変種
操作

std::decay

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
decay
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <type_traits> で定義
template< class T >
struct decay;
(C++11以降)

値渡しで関数引数を渡す際に実行される型変換と同等の型変換を実行します。形式的には

  • T が「U の配列」またはそれに参照される場合、メンバ typedef typeU* になります。
  • それ以外の場合で、T が関数型 F またはそれに参照される場合、メンバ typedef typestd::add_pointer<F>::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)>
    );
}

[編集] 関連項目

std::remove_cvstd::remove_reference を組み合わせる
(クラステンプレート) [編集]
暗黙の変換 配列からポインタへの変換、関数からポインタへの変換、lvalueからrvalueへの変換
English 日本語 中文(简体) 中文(繁體)