C++ decltype类型说明符(尾置返回类型使用)
转自https://blog.csdn.net/yhl_leo/article/details/50865552
1 基本语法
decltype 类型说明符生成指定表达式的类型。在此过程中,编译器分析表达式并得到它的类型,却不实际计算表达式的值。
语法为:
decltype( expression )
- 1
编译器使用下列规则来确定expression 参数的类型。
- 如果
expression参数是标识符或类成员访问,则decltype(expression)是expression命名的实体的类型。如果不存在此类实体或expression参数命名一组重载函数,则编译器将生成错误消息。 - 如果
expression参数是对一个函数或一个重载运算符函数的调用,则decltype(expression)是函数的返回类型。将忽略重载运算符两边的括号。 - 如果
expression参数是右值,则decltype(expression)是expression类型。如果expression参数是左值,则decltype(expression)是对 左值引用 类型的expression。
给出如下示例代码:
int var;
const int&& fx();
struct A { double x; }
const A* a = new A();
- 1
- 2
- 3
- 4
| 语句 | 类型 | 注释 |
|---|---|---|
decltype(fx()); |
const int && |
对左值引用的const int |
decltype(var); |
int |
变量 var 的类型 |
decltype(a->x); |
double |
成员访问的类型 |
decltype((a->x)); |
const double& |
内部括号导致语句作为表达式而不是成员访问计算。由于a声明为 const指针,因此类型是对const double的引用。 |
2 decltype和引用
如果decltype使用的表达式不是一个变量,则decltype返回表达式结果对应的类型。但是有些时候,一些表达式向decltype返回一个引用类型。一般来说,当这种情形发生时,意味着该表达式的结果对象能作为一条赋值语句的左值:
// decltype的结果可以是引用类型
int i = 42, *p = &i, &r = i;
decltype(r + 0) b; // OK, 加法的结果是int,因此b是一个(未初始化)的int
decltype(*p) c; // Error, c是int&, 必须初始化
- 1
- 2
- 3
- 4
因为r是一个引用,因此decltype(r)的结果是引用类型,如果想让结果类型是r所指的类型,可以把r作为表达式的一部分,如r+0,显然这个表达式的结果将是一个具体的值而非一个引用。
另一方面,如果表达式的内容是解引用操作,则decltype将得到引用类型。正如我们所熟悉的那样,解引用指针可以得到指针所指对象,而且还能给这个对象赋值,因此,decltype(*p)的结果类型是int&而非int。
3 decltype和auto
- 处理顶层const和引用的方式不同(参考阅读:C++ auto类型说明符)
如果decltype使用的表达式是一个变量,则decltype返回该变量的类型(包括顶层const和引用在内):
const int ci = 0, &cj = ci;
decltype(ci) x = 0; // x的类型是const int
decltype(cj) y = x; // y的类型是const int&,y绑定到变量x
decltype(cj) z; // Error, z是一个引用,必须初始化
- 1
- 2
- 3
- 4
decltype的结果类型与表达式形式密切相关
对于decltype所用的引用来说,如果变量名加上了一对括号,则得到的类型与不加括号时会有所不同。如果decltype使用的是一个不加括号的变量,则得到的结果就是该变量的类型;如果给变量加上了一层或多层括号,编译器就会把它当成是一个表达式。
decltype((i)) d; // Error, d是int&, 必须初始化
decltype(i) e; // OK, e是一个未初始化的int
- 1
- 2
- 模板函数的返回类型
- 在 C++11 中,可以结合使用尾随返回类型上的
decltype类型说明符和auto关键字来声明其返回类型依赖于其模板参数类型的模板函数。 - 在 C++14 中,可以使用不带尾随返回类型的
decltype(auto)来声明其返回类型取决于其模板参数类型的模板函数。
- 在 C++11 中,可以结合使用尾随返回类型上的
例如,定义一个求和模板函数:
//C++11
template<typename T, typename U>
auto myFunc(T&& t, U&& u) -> decltype (forward<T>(t) + forward<U>(u))
{ return forward<T>(t) + forward<U>(u); };
//C++14
template<typename T, typename U>
decltype(auto) myFunc(T&& t, U&& u)
{ return forward<T>(t) + forward<U>(u); };
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(forward:如果参数是右值或右值引用,则有条件地将其参数强制转换为右值引用。)
附上一段源码:
#include <iostream>
#include <string>
#include <utility>
#include <iomanip>
using namespace std;
template<typename T1, typename T2>
auto Plus(T1&& t1, T2&& t2) ->
decltype(forward<T1>(t1) + forward<T2>(t2))
{
return forward<T1>(t1) + forward<T2>(t2);
}
class X
{
friend X operator+(const X& x1, const X& x2)
{
return X(x1.m_data + x2.m_data);
}
public:
X(int data) : m_data(data) {}
int Dump() const { return m_data;}
private:
int m_data;
};
int main()
{
// Integer
int i = 4;
cout <<
"Plus(i, 9) = " <<
Plus(i, 9) << endl;
// Floating point
float dx = 4.0;
float dy = 9.5;
cout <<
setprecision(3) <<
"Plus(dx, dy) = " <<
Plus(dx, dy) << endl;
// String
string hello = "Hello, ";
string world = "world!";
cout << Plus(hello, world) << endl;
// Custom type
X x1(20);
X x2(22);
X x3 = Plus(x1, x2);
cout <<
"x3.Dump() = " <<
x3.Dump() << endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
运行结果为:
Plus(i, 9) = 13
Plus(dx, dy) = 13.5
Hello, world!
x3.Dump() = 42
- 1
- 2
- 3
- 4
参考资料:
- Microsoft Developer Network: decltype(C++)
- C++ Primer(第五版)》 Stanley B. Lippman.
C++ decltype类型说明符(尾置返回类型使用)的更多相关文章
- lamda表达式和尾置返回类型
基本lambda语法 基本形式如下: [capture](parameters) mutable ->return-type {body} [capture]:叫做捕获说明符,表示一个lambd ...
- C++11新语法糖之尾置返回类型
C++11的尾置返回类型初衷是为了方便复杂函数的声明和定义,但是当复杂度稍微提升一些的时候很明显能注意到这种设计的作用微乎其微. 首先考虑如下代码: C++ //返回指向数组的指针 auto func ...
- IdentityServer4授权类型(GrantType)对应的返回类型(ResponseType)
授权类型(GrantyType) 返回类型(ResponseType) authorization_code code implicit token implicit id_token implici ...
- C 进制 类型说明符 位运算 char类型
一 进制 1. 什么是进制 是一种计数的方式 数值的表示形式 2. 二进制 1> 特点: 只有0和1 逢2进1 2> 书写格式: 0b或者0B开头 3> %d 以带符号的十进制形式输 ...
- 实现type函数用于识别标准类型和内置对象类型
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1); } var t=type(new Number( ...
- Oracle PLSQL Demo - 16.弱类型REF游标[没有指定查询类型,已指定返回类型]
declare Type ref_cur_variable IS REF cursor; cur_variable ref_cur_variable; rec_emp scott.emp%RowTyp ...
- 数组的引用——用作形参&返回类型时
一.数组的引用 切入:可以将一个变量定义成数组的引用(这个变量和数组的类型要相同) 形式: int odd[5] = {1, 3, 5, 7, 9}; int (&arr)[5] = odd; ...
- C++ decltype类型说明符
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50865552 1 基本语法 declt ...
- Web API 2:Action的返回类型
Web API 2:Action的返回类型 Web API控制器中的Action方法有如下几种返回类型: void HttpResponseMessage IHttpActionResult 其它类型 ...
随机推荐
- PIE SDK存储格式转换
1.算法功能简介 影像存储格式转换可以实现栅格数据存储格式的自由转换,其中存储格式可以是 BSQ. BIP. BIL 三种格式. 遥感数字图像数据的存储与分发,通常采用以下三种数据格式: BSQ( ...
- proxyee down源码分析
proxyee down下载速度不错, 底层使用netty+多线程,最近在看netty网络方面的应用,正好这是个案例 源代码地址 https://github.com/proxyee-down-org ...
- resty-limit-multiple-strategy.lua
--[[ 执行过载限流策略 --]] -- 当执行限流时,Nginx 返回的状态码 err_code = local limit_config = { user_limit = {rate = , b ...
- windows 系统 python3.5安装 lxml 库
有个提示uable find vc***,的错误,如果按照修改python脚本的方法会发现还需要安装VS,安装好了还不一定可以解决问题. 费了半天劲,结合网络上部分信息终于找到了解决方案: 1.打开文 ...
- Apache Beam的基本概念
不多说,直接上干货! Apache Beam的基本概念 在使用Apache Beam构建数据处理程序,首先需要使用Beam SDK中的类创建一个Driver程序,在Driver程序中创建一个满足我们数 ...
- spark on yarn模式里需要有时手工释放linux内存
为什么要提出这个问题? spark跑YARN模式或Client模式提交任务不成功(application state: ACCEPTED) 然后执行 [spark@master spark--bin- ...
- SpringMvc上传文件遇到重复读取InputStream的问题
文件上传配置: <bean id="multipartResolver" class="org.springframework.web.multipart.comm ...
- pulic——功能性(自己写完测试的)
一. 构建一个数组[“00:00”,"00:05"..."23:55"]的数组 function buildAxis(){ var ary=[]; ary.pu ...
- TOJ 2711 Stars
描述 Astronomers often examine star maps where stars are represented by points on a plane and each sta ...
- Basic Data Structures and Algorithms in the Linux Kernel--reference
http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...