名稱空間
變體
操作

std::decay

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(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 新增特化,則行為未定義。

目錄

[編輯] 成員型別

名稱 定義
型別 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
(類模板) [編輯]
隱式轉換 陣列到指標,函式到指標,左值到右值轉換