参考资料


• 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 ;
}

std::decay的更多相关文章

  1. 【C/C++开发】C++11的模板类型判断——std::is_same和std::decay

    C++11的模板类型判断--std::is_same和std::decay 问题提出:有一个模板函数,函数在处理int型和double型时需要进行特殊的处理,那么怎么在编译期知道传入的参数的数据类型是 ...

  2. c++11::std::is_same/decay

    #include <type_traits> std::is_same 判断类型是否一致 通过std::is_same即可判断两个类型是否一样,特别在模板里面,在不清楚模板的参数时,此功能 ...

  3. 山寨一个std::bind\boost::bind

    这里是最初始的版本,参考https://github.com/cplusplus-study/fork_stl/blob/master/include/bind.hpp 提供了最简洁的实现方式. 第一 ...

  4. std::tuple作为参数invoke调用函数

    template<typename Function, typename Tuple, std::size_t... Index> decltype(auto) invoke_impl(F ...

  5. 剖析std::function接口与实现

    目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...

  6. std::bind接口与实现

    前言 最近想起半年前鸽下来的Haskell,重温了一下忘得精光的语法,读了几个示例程序,挺带感的,于是函数式编程的草就种得更深了.又去Google了一下C++与FP,找到了一份近乎完美的讲义,然后被带 ...

  7. Effective Modern C++ Item 27:重载universal references

    假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...

  8. Item 27: 明白什么时候选择重载,什么时候选择universal引用

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 Item 26已经解释了,不管是对全局函数还是成员 ...

  9. 实现UDP高效接收/响应

    环境Linux g++6.3.0 问题一:一个ip地址如何接收高并发请求 问题二:如何高并发响应消息 发送请求端只能通过ip地址+端口号向服务器发送请求码,所以服务器只能用一个UDP去绑定此ip以及端 ...

随机推荐

  1. HBase学习系列

    转自:http://www.aboutyun.com/thread-8391-1-1.html 问题导读: 1.hbase是什么? 2.hbase原理是什么? 3.hbase使用中会遇到什么问题? 4 ...

  2. php -- instanceof、class_exists、insterface_exists、method_exists、get_class、get_parent_class

    class_exists:类是否存在 在创建对象之前判断类是否存在,如果不存在就应该先加载类,再创建对象,容错. interface_exists:接口是否存在 method_exists:方法是否存 ...

  3. Angular 组件与模板 - 属性指令

    指令概览 在 Angular 中有三种类型的指令: 组件 — 拥有模板的指令 结构型指令 — 通过添加和移除 DOM 元素改变 DOM 布局的指令 属性型指令 — 改变元素.组件或其它指令的外观和行为 ...

  4. Spring中Adivisor和Aspect的差别(自我理解)

    在AOP中有几个概念: - 方/切 面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管理是J2EE应用中一个非常好的横切关注点样例. 方面用Spring的Advisor ...

  5. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

  6. win10 设置C盘访问权限

    “以管理员身份运行” cmd.exe C:\Windows\system32>icacls "C:\Program Files\Epic Games" /setintegri ...

  7. shell脚本学习总结09--分支与循环结构

    if 条件语句 = start ]];then echo start app elif [[ $ = stop ]];then echo stop appelif [[ $1 = ... ]];the ...

  8. Android无线测试之—UiAutomator UiScrollable API介绍六

    向前与向后滚动API 一.向前与向后滚动相关API 返回值 API 描述 boolean scrollBackward(int steps) 自动以步长向后滑动 boolean scrollBackw ...

  9. 第1章 部署虚拟环境安装linux系统

    章节简述: 本章节带领读者从0基础了解虚拟机软件与红帽系统,完整的演示了在VM与KVM中安装红帽RHEL7系统的方法. 特别增加了超级实用的Linux系统找回root密码.虚拟机功能增强包.VNC远程 ...

  10. uid-datepicker

    $("#txtPODate").daterangepicker({singleDatePicker: true,showDropdowns: true,minDate:'01/01 ...