decltype c++11
来源:互联网 发布:sql数据库管理网页版 编辑:程序博客网 时间:2024/06/05 14:54
Sometimes we want to define a variable with a type that the compiler deduces from a expression but do not want to use that expression to initialize the variable. For such cases, the new standard introduced a second type specifier, decltype which return the type of its operand. The compiler analyzes the expression to determine its type but does not evaluate the expression:
decltype(f()) sum = x;
Here, the compiler does not call f, but it use the type that such a call would return as the type for sum. That is, the compiler gives sum the same type as the type that would be returned if we were to call f.
The way decltype handles top-level const and references differs subtly from the way auto does. When the expression to which we apply decltype is a variable, decltype returns the type of that variable, including top-level const and references:
const int ci = 0, &cj = ci;
decltype(ci) x = 0; // x has type const int
decltype(cj) y = 0; // y has const int & and is bound to x
decltype(cj) z; // error: z is a reference and must be initialized
Because cj is a reference, decltype(cj) is a reference type. Like any other reference, z must be initialized.
It is worth noting that decltype is the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers.
decltype(f()) sum = x;
Here, the compiler does not call f, but it use the type that such a call would return as the type for sum. That is, the compiler gives sum the same type as the type that would be returned if we were to call f.
The way decltype handles top-level const and references differs subtly from the way auto does. When the expression to which we apply decltype is a variable, decltype returns the type of that variable, including top-level const and references:
const int ci = 0, &cj = ci;
decltype(ci) x = 0; // x has type const int
decltype(cj) y = 0; // y has const int & and is bound to x
decltype(cj) z; // error: z is a reference and must be initialized
Because cj is a reference, decltype(cj) is a reference type. Like any other reference, z must be initialized.
It is worth noting that decltype is the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers.
0 0
- C语言11中的auto和decltype
- [C++] decltype类型指示符
- Modern C++(三)decltype关键字
- C++11之decltype
- C++11之decltype
- C++ 11 auto & decltype
- 【c++11】 decltype
- C++ 11 auto & decltype
- C++ 11 auto & decltype
- C++11 auto&decltype
- c++11 decltype
- decltype c++11
- C++11 decltype关键字
- c++11新标准 decltype
- c++11 decltype
- C++11之decltype
- decltype
- decltype
- android复习路之LitePal篇
- Java程序员应该知道的10个eclipse调试技巧
- 优美的配色方案设计
- 基于 vuforia + unity + android studio 的AR开发(1)
- 2016年总结与2017年的期望。
- decltype c++11
- 2017我的个人目标
- Python应用中使用MongoDB方法步骤详解
- 83. Remove Duplicates from Sorted List
- 【退烧——终极追求】韧锋 Realforce静电容 87U 十周年 静音版键盘
- 关于asp下拉绑定传值问题
- POJ2186 Popular Cows 强连通分量
- 我想问下关于android app问题。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 【js类库AngularJs】解决angular+springmvc的post提交问题