std::decay
2024-09-04 10:26:28
参考资料
• cplusplus.com:http://www.cplusplus.com/reference/type_traits/decay/
• cppreference.com:http://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 ;
}
最新文章
- Android 图形总结
- 异或链表(XOR linked list)
- Linux下Ant的安装
- air mobile andriod ios 页面加载控件
- python抓取网络内容
- Go笔记-继承
- Mybatis框架基础支持层——日志模块(8)
- tensorflow源码阅读(c++)(一)
- 聚类——KFCM的matlab程序
- trajan
- robots.txt、humans.txt、.editorconfig、.gitignore、LICENSE.txt、README.md、CHANGLOG.md
- 2019-02-22 L231
- 【Alpha】第十次Scrum meeting
- OpenSSL开发学习总结
- Java springmvc 统一异常处理的方案
- gridview 绑定多个格式相同的数据源(数据查询合并)
- JDK1.7新特性(1):Switch和数字
- C语言的知识与能力的自评
- REST开放接口生成文档工具之apidoc
- 百度jQuery库