Namespaces
Variants

std:: decay

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
decay
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <type_traits>
template < class T >
struct decay ;
(C++11以降)

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

  • T が「 U の配列」またはその参照である場合、メンバーtypedef type U* となります。
  • それ以外の場合、 T が関数型 F またはその参照である場合、メンバー typedef type std:: add_pointer < F > :: type です。

プログラムが std::decay に対する特殊化を追加する場合、動作は未定義です。

目次

翻訳のポイント: - 「Contents」→「目次」に翻訳 - HTMLタグ、属性、リンク先は一切変更せず保持 - C++関連の用語(Member types, Helper types, Possible implementation, Example, See also)は原文のまま保持 - 数字や書式は完全に維持 - プロフェッショナルな技術文書としての正確性を確保

メンバー型

名前 定義
type T に decay 型変換を適用した結果

ヘルパー型

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>;
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)>
);
int main() {}

関連項目

std::remove_cv std::remove_reference を組み合わせる
(クラステンプレート)
implicit conversion 配列からポインタ、関数からポインタ、左辺値から右辺値への変換