C++ Decltype 关键字
12.1.9 C++ Decltype 关键字
12.1.9.1 问题描述
template<class T1, class T2>
void ft(T1 x, T2 y)
{...?type? xpy = x + y;//C++98 can't explain...,xpy的类型无法描述...
}
12.1.9.2 Decltype 关键字(C++11)
The C++11 solution is a new keyword: decltype.可以这样更改函数:
template<class T1, class T2>
void ft(T1 x, T2 y)
{...decltype(x + y) xpy = x + y;...
}
但是decltype是如何评估是什么类型的呢,分为四个步骤,假设有如下声明:
decltype (expression) var;
Stage 1: 如果expresion是一个没有用括号括起的标识符,则var的类型与该标识符的类型相同,包括const等限定符。
double x = 5.5;
double y = 7.9;
double &rx = x;
const double * pd;
decltype(x) w; // w is type double
decltype(rx) u = y; // u is type double &
decltype(pd) v; // v is type const double *
Stage 2: 如果expression是一个函数调用,则var的类型与函数的返回值相同。
long indeed(int);
decltype (indeed(3)) m; // m is type long
//The call expression isn’t evaluated. In this case, the compiler examines the prototype to
get the return type; there’s no need to actually call the function.
Stage 3:如果expression是一个左值,则var为指向其类型的引用。前提是expression是用括号括起的标识符。
double xx = 4.4;
decltype ((xx)) r2 = xx; // r2 is double &
decltype(xx) w = xx; // w is double (Stage 1 match)
//Incidentally, parentheses don’t change the value or lvaluedness of an expression. For
example, the following two statements have the same effect:
xx = 98.6;
(xx) = 98.6; // () don't affect use of xx
Stage 4: 如果前面的条件都不满足,则var的类型与expression的类型相同。
int j = 3;
int &k = j
int &n = j;
decltype(j+6) i1; // i1 type int
decltype(100L) i2; // i2 type long
decltype(k+n) i3; // i3 type int;
//Note that although k and n are references, the expression k+n is not a reference; it’s just
the sum of two ints, hence an int.
12.1.9.3 后置返回值类型
如何确定返回值的类型:
template<class T1, class T2>
?type? gt(T1 x, T2 y)
{...return x + y;
}
//如果继续使用decltype方式的话,当函数返回值时,x,y的存储空间已经收回了,
//所以,不知道应该是什么类型
C++11定义了新的语法:
auto h(int x, float y) -> double;
//-> double成为后置返回类型. 其中auto是一个占位符,表示后置返回类型提供的类型。
结合->和decltype,可以得出如下解决方案:
template<class T1, class T2>
auto gt(T1 x, T2 y) -> decltype(x + y)
{...return x + y;
}
//decltype在函数声明后面,因此x和y位于作用域内,可以使用他们。
C++ Decltype 关键字相关推荐
- C++11 auto和decltype关键字
auto 可以用 auto 关键字定义变量,编译器会自动判断变量的类型.例如: auto i =100; // i 是 int auto p = new A(); // p 是 A* auto k = ...
- C++decltype关键字
decltype decltype 关键字用于检查实体的声明类型或表达式的类型及值分类. 语法: decltype ( expression ) decltype 使用 // 尾置返回允许我们在参数列 ...
- C++ decltype关键字
C++ decltype关键字 希望根据表达式判定变量类型,但不用表达式的值初始化变量 如果表达式的结果对象能作为一条赋值语句的左值,则表达式将向decltype返回一个引用类型 变量加上括号后会被编 ...
- C++ auto和decltype关键字
可以用 auto 关键字定义变量,编译器会自动判断变量的类型.例如: auto i =100; // i 是 int auto p = new A(); // p 是 A* auto k = 3434 ...
- C++11新特性之decltype关键字的使用
一.decltype关键字介绍 decltype关键字与auto关键字相似,但又有不同之处:auto关键字是在编译时通过已经初始化的变量来确定auto所代表的类型.换句话说,auto修饰的表达式必须是 ...
- decltype关键字详解
学习目标: 掌握c++ decltype关键字 学习内容: decltype 是 C++11 新增的一个关键字,它和 auto 的功能一样,都用来在编译时期进行自动类型推导. 既然已经有了 auto ...
- 理解 decltype关键字
1. decltype关键字 decltype被称作类型说明符,它的作用是选择并返回操作数的数据类型. 例如 Test2函数的返回值是std::initializer_list类型 std::init ...
- int指针初始化_C++:变量,指针,引用const,extern,using,typedef,decltype关键字
算数类型 基本类型就是int,double, long long,这一系列东西. 其中有个特殊的类型是wchar,这个符号代表本机上支持的最大的扩展字符级的字符. (有的机器上有些扩展字符级比较大,所 ...
- C++关键字decltype
decltype 关键字用于检查实体的声明类型或表达式的类型及值分类. 语法 decltype ( expression ) 使用 // 尾置返回允许我们在参数列表之后声明返回类型 template ...
最新文章
- python旋转矩阵_python实现回旋矩阵方式(旋转矩阵)
- 2018ACM-ICPC Asia Nanjing Regional Contest
- mysql 修改自增字段起始值不生效_Mysql数据库基本介绍
- Hexo 入门指南(七) - 评论 分享
- Java并发编程框架Disruptor
- 使用Lambda解决_inbound_nodes错误
- 【优化求解】基于matlab蚁群算法的函数优化分析【含Matlab源码 219期】
- 数据库,SQL,万恶之源?
- laravel 下载文件
- 网站访问数据统计工具
- JQuery日记_5.14 Sizzle选择器(七)
- CSS单行文本溢出显示省略号(…)
- ubuntu下载速度慢的解决办法--修改下载源
- docker开启远程访问后,重启失败的解决方案
- 兵临城下 (深度搜索)
- HTML5前端常用开发框架
- nginx禁止国外IP访问网站
- JVM 新生代老年代与GC
- 【】 Intel(R) 800 Series序列网卡 ice 驱动安装
- 批发零售业的得力助手(进销存商城系统)