Std::Decay 简介

传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录

1 类模板声明


// cplusplus.com
template <class T> struct decay;

// MS C++ 2013
template <class _Ty>
struct decay
{ // determines decayed version of _Ty
    ...
};

// GCC 4.8.2
template <typename _Tp>
class decay
{
    ...
}; 

2 Std::Decay 类模板描述

Type T 应用左值到右值 (LVALUE-TO-RVALUE)、Array-to-Pointer with Function-to-Pointer隐式转换。转换将删除类型 T的 CV 限定符(Const 和 Volatile 限定符)并将结果类型的类型定义为成员衰减 < T >::Type。这种转换与传输所有参数时的转换非常相似。

  • 如果类型 T 是函数类型,将应用从函数到指针的类型转换,T 的衰减类型等价于: add_pointer::type
  • 如果类型 T 是数组类型,将应用从数组到指针的类型转换,T 的衰减类型等价于add_pointer<remove_extent<remove_reference::type>::type>::type
  • 将左值应用到右值时,T 的衰减类型等价于:remove_cv<remove_reference::type>::type

3 模板参数说明

   T : 某种​​类型。当t为引用类型时,DECAY<T>::Type返回T引用的元素类型;当 T 为非引用类型时,DECAY <T>::Type 返回 T 类型。

4 Std :: Decay基本类型示例

#include <iostream>
#include <type_traits>
using namespace std;

typedef decay<int>::type         A;                                     // A is int
typedef decay<int &>::type       B;                                     // B is int
typedef decay<int &&>::type      C;                                     // C is int
typedef decay<const int &>::type D;                                     // D is int
typedef decay<int[2]>::type      E;                                     // E is int *
typedef decay<int(int)>::type    F;                                     // F is int(*)(int)

int main()
{
    cout << boolalpha;
    cout << is_same<int, A>::value         << endl;                     // true
    cout << is_same<int, B>::value         << endl;                     // true
    cout << is_same<int, C>::value         << endl;                     // true
    cout << is_same<int, D>::value         << endl;                     // true
    cout << is_same<int *, E>::value       << endl;                     // true
    cout << is_same<int(int), F>::value    << endl;                     // false
    cout << is_same<int(*)(int), F>::value << endl;                     // true

    return 1;
}

5 Std :: Decay非基本类型示例

#include <iostream>
#include <type_traits>
using namespace std;

class MyClass {};

typedef decay<MyClass>::type         A;                                 // A is MyClass
typedef decay<MyClass &>::type       B;                                 // B is MyClass
typedef decay<MyClass &&>::type      C;                                 // C is MyClass
typedef decay<const MyClass &>::type D;                                 // D is MyClass
typedef decay<MyClass[2]>::type      E;                                 // E is MyClass *
typedef decay<MyClass *>::type       F;                                 // E is MyClass *
typedef decay<MyClass *[2]>::type    G;                                 // G is MyClass **
typedef decay<MyClass **>::type      H;                                 // H is MyClass **

int main()
{
    cout << boolalpha;
    cout << is_same<MyClass, A>::value    << endl;                      // true
    cout << is_same<MyClass, B>::value    << endl;                      // true
    cout << is_same<MyClass, C>::value    << endl;                      // true
    cout << is_same<MyClass, D>::value    << endl;                      // true
    cout << is_same<MyClass *, E>::value  << endl;                      // true
    cout << is_same<MyClass *, F>::value  << endl;                      // true
    cout << is_same<MyClass **, G>::value << endl;                      // true
    cout << is_same<MyClass **, H>::value << endl;                      // true

    return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果Autosar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值