std::decay
参考资料
• 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 ;
}
std::decay的更多相关文章
- 【C/C++开发】C++11的模板类型判断——std::is_same和std::decay
C++11的模板类型判断--std::is_same和std::decay 问题提出:有一个模板函数,函数在处理int型和double型时需要进行特殊的处理,那么怎么在编译期知道传入的参数的数据类型是 ...
- c++11::std::is_same/decay
#include <type_traits> std::is_same 判断类型是否一致 通过std::is_same即可判断两个类型是否一样,特别在模板里面,在不清楚模板的参数时,此功能 ...
- 山寨一个std::bind\boost::bind
这里是最初始的版本,参考https://github.com/cplusplus-study/fork_stl/blob/master/include/bind.hpp 提供了最简洁的实现方式. 第一 ...
- std::tuple作为参数invoke调用函数
template<typename Function, typename Tuple, std::size_t... Index> decltype(auto) invoke_impl(F ...
- 剖析std::function接口与实现
目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...
- std::bind接口与实现
前言 最近想起半年前鸽下来的Haskell,重温了一下忘得精光的语法,读了几个示例程序,挺带感的,于是函数式编程的草就种得更深了.又去Google了一下C++与FP,找到了一份近乎完美的讲义,然后被带 ...
- Effective Modern C++ Item 27:重载universal references
假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...
- Item 27: 明白什么时候选择重载,什么时候选择universal引用
本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 Item 26已经解释了,不管是对全局函数还是成员 ...
- 实现UDP高效接收/响应
环境Linux g++6.3.0 问题一:一个ip地址如何接收高并发请求 问题二:如何高并发响应消息 发送请求端只能通过ip地址+端口号向服务器发送请求码,所以服务器只能用一个UDP去绑定此ip以及端 ...
随机推荐
- C++ 运算符重载一(二元运算符重载)
//二元运算符重载 #include<iostream> using namespace std; class Point { public: Point(int x,int y){ th ...
- imx6 MfgTool分析
解析freescale的MfgTool中的脚本,了解imx6, android系统的分区情况. 配置文件 1. cfg.ini [profiles] chip = MX6DL Linux Update ...
- Spring Cloud 架构
我们从整体来看一下Spring Cloud主要的组件,以及它的访问流程 1.外部或者内部的非Spring Cloud项目都统一通过API网关(Zuul)来访问内部服务. 2.网关接收到请求后,从注册中 ...
- 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...
- 【代码备份】原图降采样后进行NLM滤波
文件路径: 滤波算法main.m: %% 测试函数 %NLM滤波及滤波与 clc,clear all,close all; ima_ori=double(imread('F:\Users\****n\ ...
- React-Native 样式指南
https://github.com/doyoe/react-native-stylesheet-guide
- C#中Uri类的解释
URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体的UR ...
- Mongodb默认开启与关闭
默认启动: $ ./mongodb 默认数据保存路径:/data/db/ 默认端口:27017 修改默认路径: --dbpath $ ./mongdb --dbpath /mongod ...
- HttpModule,HttpContext,HttpHandler
http://www.cnblogs.com/wujy/tag/ASP.NET%E5%9F%BA%E7%A1%80/ http://www.th7.cn/Program/net/2011/12/26/ ...
- java基础---->java自带的xml解析
在查看公司框架的源码的时候,发现框架用的是jdk自带的xml解析.今天,想着对它总结一下.从未放弃过爱你,只是从浓烈变得悄无声气. 利用jdk自带的xml创建文档 一. CreateXmlFile类如 ...