decltype关键字:
1.计算表达式的类型
sizeof操作符的值是一个整数,表示类型的长度(字节数)
typeid操作符的值是一个对象,其中包含了类型的信息
decltype操作符的值是一个类型,可用于其它对象的声明

 1 #include <iostream>
 2 #include <typeinfo>
 3 using namespace std;
 4 int main (void)
 5 {
 6    int a = 0;
 7    //int b = 1;
 8   //auto b = a;
 9    //b:int,decltype的值就是表达式的类型本身
10    decltype (a) b = 1;
11    cout << typeid (b).name () << endl; // i
12   // c:int,decltype只在编译期计算表达式的类型,不在运行期计算表达式的值
13   decltype (a++) c = 2;
14   cout << typeid (c).name () << endl; // i
15   cout << a << endl; // 0
16   int const& d = a;
17   // e:int const&,decltype会保留表达式的引用属性和CV限定
18   decltype (d) e = d;
19   cout << &e << ' ' << &a << endl; // 地址相同
20   /* e带有常属性
21   cout << ++e << endl; */
22   //f:int,auto会丢弃表达式的引用属性和CV限定
23   auto f = d;
24   cout << &f << ' ' << &a << endl; // 地址不同
25   cout << ++f << endl; // 1
26   //g:int*,h:int**,decltype可以和指针联用
27   decltype (a) *g= &a, **h = &g;
28   cout << typeid (g).name () << endl; // Pi
29   cout << typeid (h).name () << endl; // PPi
30   // h---->g---->a
31   //int** int* int
32   //i:int const&,decltype可以和引用以及CV限定联用
33   decltype (a) const& i = a;
34   cout << &i << ' ' << &a << endl; // 地址相同
35   /* i带有常属性
36   cout << ++i << endl; */
37   return 0;
38 }

2.对于函数表达式,decltype将返回该函数返回值的类型,对于左值表达式,decltype返回该表达式的左值引用

 1 #include <iostream>
 2 #include <typeinfo>
 3 using namespace std;
 4 int max (int x, int y)
 5 {
 6   return x < y ? y : x;
 7 }
 8 int* max (int* x, int* y)
 9 {
10   return *x < *y ? y : x;
11 }
12 int const& min (int const& x, int const& y)
13 {
14   return x < y ? x : y;
15 }
16 int main (void)
17 {
18   int a = 123, b = 456;
19   decltype (max (a, b)) c;
20   cout << typeid (a).name () << endl; // i
21   decltype (max (&a, &b)) d = NULL;
22   cout << typeid (d).name () << endl; // Pi
23   // e:int&
24   decltype (++a) e = a;
25   --e; // --a;
26   cout << a << endl; // 122
27   // f:int
28   decltype (b++) f = b;
29   --f;
30   cout << b << endl; // 456
31   // g:int&
32   decltype (a = b) g = a;
33   --g;
34   cout << a << endl; // 121
35   // h:int
36   decltype (b + a) h = b;
37   --h;
38   cout << b << endl; // 456
39   // i:int
40   decltype (a) i = a;
41   // j:int&
42   decltype ((a)) j = a;//decltype的表达式如果是加上了括号的变量,结果将是引用
43   cout << &i << ' ' << &j << ' ' << &a << endl;
44   return 0;
45 }

注意:decltype((variable))(注意是双层括号)的结果永远是引用,
而decltype(variable)的结果只有当variable本身是一个引用时才是引用

3.何时使用decltype

#include <iostream>
#include <vector>
#include <list>
#include <map>
using namespace std;
template<typename C>
void print(C& c)
{for (decltype (c.begin()) it = c.begin(); it != c.end(); ++it)cout << *it << ' ';cout << endl;
}
int main(void)
{int ai[] = { 10, 20, 30, 40, 50 };vector<int> vi(ai, ai + 5);print(vi);list<int> const li(vi.begin(), vi.end());print(li);map<string, vector<int> > msv;msv["张飞"].push_back(70);msv["张飞"].push_back(85);msv["赵云"].push_back(75);msv["赵云"].push_back(90);msv["关羽"].push_back(80);msv["关羽"].push_back(95);// .// .// .// 此处省略15000行代码// .// .// .//    int sum = 0;//key_type就表示map中key的类型,value_type表示具体值的类型,mapped_type表示map中value(pair)的类型decltype (msv)::mapped_type::value_type sum = 0;for (size_t i = 0; i < msv["张飞"].size(); ++i)sum += msv["张飞"][i];cout << sum << endl;return 0;
}

4.auto和decltype结合使用,返回类型后置

#include <iostream>
#include <typeinfo>
using namespace std;
double foo(int arg)
{return arg / 2.0;
}
int foo(double arg)
{return int(arg * 2);
}
// 返回类型后置
template<typename T>
auto bar(T const& arg) -> decltype (foo(arg))
{return foo(arg);
}
// 返回类型后置
template<typename T>
auto add(T const& x, T const& y) -> decltype (x + y)
{return x + y;
}class Point
{
public:Point(int x, int y) : m_x(x), m_y(y) {}void draw(void) const{cout << "点(" << m_x << ',' << m_y << ')' << endl;}
private:int m_x, m_y;
};class Line
{
public:Line(Point const& p1, Point const& p2) :m_p1(p1), m_p2(p2) {}void draw(void) const{cout << "线(" << '\t';m_p1.draw();cout << '\t';m_p2.draw();cout << ')' << endl;}
private:Point m_p1, m_p2;
};Line const operator+ (Point const& p1, Point const& p2)
{return Line(p1, p2);
}
int main(void)
{cout << bar(1) << endl; // 0.5cout << bar(0.5) << endl; // 1Point p1(100, 200), p2(300, 400);Line line = add(p1, p2);line.draw();return 0;
}

转载于:https://www.cnblogs.com/LuckCoder/p/8467634.html

decltype关键字相关推荐

  1. C++11 auto和decltype关键字

    auto 可以用 auto 关键字定义变量,编译器会自动判断变量的类型.例如: auto i =100; // i 是 int auto p = new A(); // p 是 A* auto k = ...

  2. C++decltype关键字

    decltype decltype 关键字用于检查实体的声明类型或表达式的类型及值分类. 语法: decltype ( expression ) decltype 使用 // 尾置返回允许我们在参数列 ...

  3. C++ decltype关键字

    C++ decltype关键字 希望根据表达式判定变量类型,但不用表达式的值初始化变量 如果表达式的结果对象能作为一条赋值语句的左值,则表达式将向decltype返回一个引用类型 变量加上括号后会被编 ...

  4. C++ auto和decltype关键字

    可以用 auto 关键字定义变量,编译器会自动判断变量的类型.例如: auto i =100; // i 是 int auto p = new A(); // p 是 A* auto k = 3434 ...

  5. C++11新特性之decltype关键字的使用

    一.decltype关键字介绍 decltype关键字与auto关键字相似,但又有不同之处:auto关键字是在编译时通过已经初始化的变量来确定auto所代表的类型.换句话说,auto修饰的表达式必须是 ...

  6. C++ Decltype 关键字

    12.1.9 C++ Decltype 关键字 12.1.9.1 问题描述 template<class T1, class T2> void ft(T1 x, T2 y) {...?ty ...

  7. decltype关键字详解

    学习目标: 掌握c++ decltype关键字 学习内容: decltype 是 C++11 新增的一个关键字,它和 auto 的功能一样,都用来在编译时期进行自动类型推导. 既然已经有了 auto ...

  8. 理解 decltype关键字

    1. decltype关键字 decltype被称作类型说明符,它的作用是选择并返回操作数的数据类型. 例如 Test2函数的返回值是std::initializer_list类型 std::init ...

  9. int指针初始化_C++:变量,指针,引用const,extern,using,typedef,decltype关键字

    算数类型 基本类型就是int,double, long long,这一系列东西. 其中有个特殊的类型是wchar,这个符号代表本机上支持的最大的扩展字符级的字符. (有的机器上有些扩展字符级比较大,所 ...

  10. C++关键字decltype

    decltype 关键字用于检查实体的声明类型或表达式的类型及值分类. 语法 decltype ( expression ) 使用 // 尾置返回允许我们在参数列表之后声明返回类型 template ...

最新文章

  1. 我的机器学习入门清单及路线!
  2. DP_字串匹配(HDU_1501)
  3. python编程神器下载_Python编程神器 -程序员必备开发手册
  4. 算法分析 运动员循环赛_「98跑」大众跑者的训练比专业运动员更累!
  5. 2015-12-15 关于数量个
  6. 数据结构-队列之顺序队列
  7. python中seek函数的用法_在Python中操作文件之seek()方法的使用教程
  8. 《机器人学导论--Join J.Craig》第一章 绪论
  9. android 设备实现定时重启(无root权限或已root)
  10. 基于JAVA宠物管理系统的设计与实现
  11. 高性能Nginx服务器+互联网高并发解决方案+安全架构 蚂蚁学堂互联网架构师课程
  12. selenium上传附件的两种方式(普通上传和借助AutoIt识别Windows上传窗口)
  13. centos 安装frp 实现内网穿透进行电信物联网NBIOT开发
  14. SDN控制器与交换机如何建立连接
  15. Typhon升级到5.8,编译原来程序出现提示:Compilation raised exception internally
  16. SEO精准搜索流量的玩法
  17. latex小技巧—极限符号下方分成两行
  18. SPA 项目 之 后台接口文档
  19. 基于Netty的UDP服务端开发
  20. Spring源码:Advice接口

热门文章

  1. mysql查询条件中使用 或 !-的问题
  2. 分享网上一篇产品经理的经验总结--产品经理九步法
  3. POJ 2991 Crane
  4. 51Nod 1637 幸运数字转换(思维)
  5. linux抓包命令不用root用户,linux中非root用户使用wireshark进行抓包
  6. pandas获取dataframe的行数,列数,元素个数
  7. hive 时间函数 总结
  8. 《WebGL编程指南》学习笔记——1.WebGL概述
  9. Mybatis 查询出来的数据数量正确,但是具体数据为null
  10. image 第二次使用就出错是怎么回事_第二次上机报告-RNA-seq (HISAT - SAMtools- StringTie - ballgown)amp;amp;Gene-Assembly