VS2015下测试:

decltype:

class Foo {};

int &func_int_r(void) { int i = 0; return i; };
int &&func_int_rr(void) { return 0; };
int func_int(void) { return 0; }; const int &func_cint_r(void) { int i = 0; return i; };
const int &&func_cint_rr(void) { return 0; };
const int func_cint(void) { return 0; };
const Foo func_cfoo(void) { return Foo(); }; int main()
{
{
int x = 0;
decltype(func_int_r()) al = x; // al -> int &
decltype(func_int_rr()) bl = 0; // bl -> int &&
decltype(func_int()) cl = 0; // cl -> int decltype(func_cint_r()) a2 = x; // a2 -> const int &
decltype(func_cint_rr()) b2 = 0; // b2 -> const int &&
decltype(func_cint()) c2 = 0; // c2 -> int
decltype(func_cfoo()) d2 = Foo(); // d2 -> Foo decltype((x)) t = x; // t -> int &
decltype(x + 1) t2; // t2 -> int 表达式
decltype(++x) t3 = x; // t3 -> int & 表达式返回左值 decltype((++x)) t4 = x; // t6 -> int & decltype((1)) t5 = x; // t4 -> int
decltype(1) t6 = x; // t5 -> int int i = 0;
} system("pause");
return 0;
}
  • 当函数返回的是右值时,decltype会摒弃cv操作符。c2,d2
  • 当标识符加上()后,decltype结果为左值引用。t,t4
  • 当表达式赋值给左值时,decltype结果为左值引用。t3

    -其它按简单推导即可。

    应用:
template<class ContainerT>
class Foo
{
typename ContainerT::iterator it_; //类型定义可能有问题
public:
void func(ContainerT& container)
{
it_ = container.begin();
}
}; int main()
{
typedef const vector<int> container_t;
container_t arr; Foo<container_t> foo;
foo.func(arr); system("pause");
return 0;
}

编译时报错,因为当ContainerT是一个const时,it_应该是const_iterator;解决方法:

template<class ContainerT>
class Foo
{
private:
//decltype(ContainerT()::begin());//在vs2015中,ContainerT()会被推导为vector<int>类型,const被摒弃,所以出错。
static ContainerT gi;
decltype(gi.begin()) it_; //不会执行表达式,只会推导,跟auto不同
public:
void func(ContainerT& container)
{
decltype(ContainerT()) tt;
it_ =container.begin();
int i = 0;
}
}; int main()
{
using container_t= const vector<int> ;
container_t arr; Foo<container_t> foo;
foo.func(arr); system("pause");
return 0;
}

返回类型后置语法:

template<typename U,typename R>
auto add(U u, R r)->decltype(u + r)
{
return u + r;
} int main()
{
cout << add(100, 100.0f) << endl;
cout<<add<int,float>(100, 100.0f) << endl;
system("pause");
return 0;
}

auto:

  • 当auto后面显式添加&时,才会保留表达式cv限定符和引用。
  • 当auto推导结果为指针时,保留cv操作符。
  • 其它情况不保留cv限定符和引用。
int main()
{ int x = 0;
const auto* a = &x; // a -> const int *
auto b = &x; // b -> int *
auto &c = x; // c -> int &
auto d = c; // d -> int
const auto e = x; // e -> const int
auto f = e; // f -> int const auto& g = x; // g -> const int &
auto& h = g; // h -> const int &
const auto i = g; // i -> const int
auto j = a; // j -> const int * const int ci = i, &cr = ci;
auto d1 = &i; // d1 -> const int *
auto e1 = &ci; // e1 -> const int * system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++11 auto和decltype推导规则的更多相关文章

  1. c++11 auto 与 decltype 详解

    转自: here 一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题 ...

  2. C++11 auto and decltype

    1.auto关键字 C++新标准引入auto关键词,此auto与之前C语言的auto意义已经不一样了. 这里的auto是修饰未知变量的类型,编译器会通过此变量的初始化自动推导变量的类型. 例如:aut ...

  3. c++11——auto,decltype类型推导

    c++11中引入了auto和decltype关键字实现类型推导,通过这两个关键字不仅能够方便的获取复杂的类型,而且还能简化书写,提高编码效率.     auto和decltype的类型推导都是编译器在 ...

  4. C++ 11 学习1:类型自动推导 auto和decltype

    Cocos 3.x 用了大量的C++ 11 的东西,所以作为一个C++忠实粉丝,有必要对C++ 11进行一个系统的学习. 使用C++11之前,一定要注意自己使用的编译器对C++11的支持情况,有些编译 ...

  5. C++11 图说VS2013下的引用叠加规则和模板参数类型推导规则

    背景:    最近在学习C++STL,出于偶然,在C++Reference上看到了vector下的emplace_back函数,不想由此引发了一系列的“探索”,于是就有了现在这篇博文. 前言:     ...

  6. 【C++】C++11的auto和decltype关键字

    转自: http://www.linuxidc.com/Linux/2015-02/113568.htm 今天要介绍C++11中两个重要的关键字,即auto和decltype.实际上在C++98中,已 ...

  7. C++11新特性— auto 和 decltype 区别和联系

    一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准 ...

  8. Effective Modern C++ ——条款2 条款3 理解auto型别推导与理解decltype

    条款2.理解auto型别推导 对于auto的型别推导而言,其中大部分情况和模板型别推导是一模一样的.只有一种特例情况. 我们先针对auto和模板型别推导一致的情况进行讨论: //某变量采用auto来声 ...

  9. C++ 11 新特性: auto 和 decltype 区别和联系

    一. auto简介编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准就 ...

随机推荐

  1. Oracle 数据库 INTERVAL DAY TO SECOND类型的使用

    INTERVAL DAY TO SECOND类型可以用来存储单位为天和秒的时间间隔.下面这条语句创建一个名为promotions的表,用来存储促销信息.promotions表包含了一个INTERVAL ...

  2. Windows 7安装PHP运行环境和开发环境

    1. 安装Apache 下载地址:http://www.apache.org/dyn/closer.cgi/httpd/binaries/win32 如需更改端口:打开Apache安装目录下conf目 ...

  3. 一个专为电商定制的域名.shop

    2.73亿元人民币获得.shop域名的经营权,使shop域名成为最高节拍价的顶级域名.虽然最终“最高节拍价”被web域名打破,但在电商届域名里shop还是王者.shop作为一个主要面向线上.线下销售实 ...

  4. php中浮点型变量

    $d = (0.1+0.7)*10;//$d为8 //floor函数返回不大于value的最接近的整数,舍去小数部分取整 $e = floor($d);//$e为7 为什么$e为7,而不是8 因为$d ...

  5. 【转载】JAVA中线程的两种实现方法-实现Runnable接口和继承Thread类

    转自: http://blog.csdn.net/sunguangran/article/details/6069317 非常感谢原作者,整理的这么详细. 在java中可有两种方式实现多线程,一种是继 ...

  6. spring: 在表达式中使用类型

    如果要在SpEL中访问类作用域的方法和常量的话,要依赖T()这个关键的运算符.例如,为了在SpEL中表达Java的Math类,需要按照如下的方式使用T()运算符: T{java.lang.Math} ...

  7. Effective Objective-C手记

    类型常量:使用类型常量代替#define 1. 普通常量 //.m #import "xxxxx.h" static const NSTimerInterval kAnimatio ...

  8. java多线程学习一

    声明:本篇博客是本人为了自己学习保存的心得,其内容主要是从大神——五月的仓颉的博客中学习而来,在此多谢大神五月的仓颉的分享,敬礼! 第一章:进程和线程的概念 进程:进程是操作系统中作为分配资源的基本单 ...

  9. 51nod 1102 单调栈

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 1102 面积最大的矩形 基准时间限制:1 秒 空间限制:1310 ...

  10. 慕课网python分布式爬虫打造搜索引擎视频中爬取伯乐网文章

    代码:https://github.com/longbigbeard/scrapy_demo