decltype
返回值 decltype(表达式)
[返回值的类型是表达式参数的类型]
这个可也用来决定表达式的类型,就像Bjarne暗示的一样,如果我们需要去初始化某种类型的变量,auto是最简单的选择,但是如果我们所需的类型不是一个变量,例如返回值这时我们可也试一下decltype。
现在我们回看一些例子我们先前做过的,
- template <class U, class V>
- void Somefunction(U u, V v)
- {
- result = u*v;//now what type would be the result???
- decltype(u*v) result = u*v;//Hmm .... we got what we want
- }
在下面的一个段落我将会让你熟悉这个观念用 auto 和 decltype 来声明模板函数的返回值,其类型依靠模板参数。
1. 如果这个表达式是个函数,decltype 给出的类型为函数返回值的类型。
- int add(int i, int j){ return i+j; }
- decltype(add(5,6)) var = 5;//Here the type of var is return of add() -> which is int
2.如果表达式是一个左值类型,那么 decltype 给出的类型为表达式左值引用类型。
- struct M { double x; };
- double pi = 3.14;
- const M* m = new M();
- decltype( (m->x) ) piRef = pi;
- // Note: Due to the inner bracets the inner statement is evaluated as expression,
- // rather than member 'x' and as type of x is double and as this is lvale
- // the return of declspec is double& and as 'm' is a const pointer
- // the return is actually const double&.
- // So the type of piRef is const double&
3.非常重要的标记一下,decltype 不会执行表达式而auto会,他仅仅推论一下表达式的类型。
- int foo(){}
- decltype( foo() ) x; // x is an int and note that
- // foo() is not actually called at runtime
跟踪返回类型:
这对 C++ 开发者来说是一个全新的特性,直到现在函数的返回类型必须放在函数名的前面。到了 C++11,我们也可以将函数返回值的类型放在函数声明后,当然仅需要用 auto 替代返回类型。现在我们想知道怎么做,让我们来寻找答案:
- template<class U, class V>
- ??? Multiply(U u, V v) // how to specifiy the type of the return value
- {
- return u*v;
- }
我们明显的不能像这样:
- template<class U, class V>
- decltype(u*v) Multiply(U u, V v) // Because u & v are not defined before Multiply.
- // What to do...what to do !!!
- {
- return u*v;
- }
这种情况我们可也使用 auto 然后当我们使用 decltype(u*v) 作为返回值这个类型便知晓了.
这是不是很酷?
- template<class U, class V>
- auto Multiply(U u, V v) -> decltype(u*v) // Note -> after the function bracet.
- {
- return u*v;
- }
decltype的更多相关文章
- C++11特性:decltype关键字
decltype简介 我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应ty ...
- C++11 auto and decltype
1.auto关键字 C++新标准引入auto关键词,此auto与之前C语言的auto意义已经不一样了. 这里的auto是修饰未知变量的类型,编译器会通过此变量的初始化自动推导变量的类型. 例如:aut ...
- C++ 11 Template ... 与Decltype 测试
#include <iostream> #include "string" using namespace std; template<typename T> ...
- 不支持C++11 decltype的噩耗
前言:因为公司现在使用vs2008,所以很多c++11的新特性还未能使用,导致写了很多冤枉代码. 最初引擎的数学库非常简单,使用起来也不方便,例如: float FastLerp(const floa ...
- auto与decltype
今天搜狗笔试的一道选择题,原题给忘了,但记得所考的知识点.知识点很基础,但很容易忽视. 具体内容可参考C++ Primer. auto :变量取auto后,其所对应的类型 auto一般会 ...
- Effective Modern C++翻译(4)-条款3:了解decltype
条款3 了解decltype decltype是一个有趣的东西,给它一个变量名或是一个表达式,decltype会告诉你这个变量名或是这个表达式的类型,通常,告诉你的结果和你预测的是一样的,但是偶尔的结 ...
- auto和decltype
auto 1.编译器通过分析表达式的类型来确定变量的类型,所以auto定义的变量必须有初始值. auto i=; //ok,i为整型 auto j; //error,定义时必须初始化. j=; ...
- C++ Prime:decltype类型指示符
decltype作用是选择并返回操作数的数据类型. decltype(f()) sum = x; // sum的类型就是函数f的返回类型 如果decltype使用的表达式是一个变量,则decltype ...
- 关于auto和decltype
auto会忽略顶层const,保留底层const ; const int* const p = &i; auto p2 = p; //p2是const int*,不是const int* co ...
随机推荐
- Android 使用Parcelable序列化对象
转:http://ipjmc.iteye.com/blog/1314145 Android序列化对象主要有两种方法,实现Serializable接口.或者实现Parcelable接口.实现 ...
- POJ1037A decorative fence(动态规划+排序计数+好题)
http://poj.org/problem?id=1037 题意:输入木棒的个数n,其中每个木棒长度等于对应的编号,把木棒按照波浪形排序,然后输出第c个; 分析:总数为i跟木棒中第k短的木棒 就等于 ...
- 打印多边形的菱形(for的嵌套)
Console.WriteLine("请输入一个数字,会出现一个多边的菱形:"); int n = Convert.ToInt32(Console.ReadLine()); ; i ...
- MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer
用MyBatis进行查询,传入参数只有一个时(非Map)如int,报错 There is no getter for property named 'sleevetype' in 'class jav ...
- MyBatis 用户表记录数查询
搭建MyBatis开发环境,实现用户表记录数查询 1.在MyEclipse中创建工程,导入MyBatis的jar包
- linux 下如何给用户添加权限
linux 添加用户.权限:# useradd –d /usr/sam -m sam此命令创建了一个用户sam,其中-d和-m选项用来为登录名sam产生一个主目录/usr/sam(/usr为默认的用户 ...
- thinkphp中limit方法
limit方法也是模型类的连贯操作方法之一,主要用于指定查询和操作的数量,特别在分页查询的时候使用较多.ThinkPHP的limit方法可以兼容所有的数据库驱动类的. 用法 限制结果数量 例如获取满足 ...
- Spring AOP使用整理:使用@AspectJ风格的切面声明
要启用基于@AspectJ风格的切面声明,需要进行以下的配置: <!-- 启用@AspectJ风格的切面声明 --> <aop:aspectj-autoproxy proxy-tar ...
- Eclipse的模板设置代码
Eclipse Java注释模板设置详解 设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后 ...
- AMD64与IA64的区别
其实很多人从字面上,都以为AMD64就是针对AMD CPU的,IA64是针对INTEL CPU,其实是错的,我最初也是这样认为,其实不然: 你在市面上买的到的intel 64位 CPU都属于amd64 ...