参考资料


• cplusplus.comhttp://www.cplusplus.com/reference/type_traits/decay/

• cppreference.comhttp://en.cppreference.com/w/cpp/types/decay

std::decay简介


• 类模板声明

// 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
{
...
};

• 类模板说明

为类型T应用从左值到右值(lvalue-to-rvalue)数组到指针(array-to-pointer)函数到指针(function-to-pointer)的隐式转换。转换将移除类型T的cv限定符(const和volatile限定符),并定义结果类型为成员decay<T>::type的类型。这种转换很类似于当函数的所有参数按值传递时发生转换。

▶ 如果类型T是一个函数类型,那么从函数到指针的类型转换将被应用,并且T的衰变类型等同于:

                  add_pointer<T>::type

▶ 如果类型T是一个数组类型,那么从数组到指针的类型转换将被应用,并且T的衰变类型等同于:

                  add_pointer<remove_extent<remove_reference<T>::type>::type>::type

▶ 当左值到右值转换被应用时,T的衰变类型等同于:

                  remove_cv<remove_reference<T>::type>::type

• 模板参数说明

T : 某种类型。当T是引用类型,decay<T>::type返回T引用的元素类型;当T是非引用类型,decay<T>::type返回T的类型。

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[]>::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 ;
}

• 非基本类型

#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[]>::type E; // E is MyClass *
typedef decay<MyClass *>::type F; // E is MyClass *
typedef decay<MyClass *[]>::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. Android 图形总结
  2. 异或链表(XOR linked list)
  3. Linux下Ant的安装
  4. air mobile andriod ios 页面加载控件
  5. python抓取网络内容
  6. Go笔记-继承
  7. Mybatis框架基础支持层——日志模块(8)
  8. tensorflow源码阅读(c++)(一)
  9. 聚类——KFCM的matlab程序
  10. trajan
  11. robots.txt、humans.txt、.editorconfig、.gitignore、LICENSE.txt、README.md、CHANGLOG.md
  12. 2019-02-22 L231
  13. 【Alpha】第十次Scrum meeting
  14. OpenSSL开发学习总结
  15. Java springmvc 统一异常处理的方案
  16. gridview 绑定多个格式相同的数据源(数据查询合并)
  17. JDK1.7新特性(1):Switch和数字
  18. C语言的知识与能力的自评
  19. REST开放接口生成文档工具之apidoc
  20. 百度jQuery库

热门文章

  1. Spring Boot简化了基于Spring的应用开发
  2. Python3x 爬取妹子图
  3. 学习《深入理解C#》—— 可空类型、可选参数和默认值 (第一章1.3)
  4. 下载Qt安装包
  5. Windows游戏编程大师技巧之三角形填充
  6. JZOJ.5315【NOIP2017模拟8.19】小串串
  7. 单台centos7.3 虚拟机实现主从复制和哨兵集群
  8. Gson简要使用笔记(转载)
  9. LeetCode 笔记系列四 Remove Nth Node From End of List
  10. Struts2中的类型转换失败