条款5 相对显式类型声明,更倾向使用auto

基础知识

  auto能大大方便变量的定义,可以表示仅由编译器知道的类型。

template<typename It>
void dwim(It b, It e) {
while(b != e) {
//typename std::iterator_traits<It>::value_type currValue = *b; // old type
auto currValue = *b; // new type
}
}

  auto可以用来定义函数,如下:

auto derefURLess = [](const std::unique_ptr<Widget>& p1, const std::unique_ptr<Widget>& p2) {
return *p1 < *p2;
} // C++ 11
auto derefLess = [](const auto& p1, const auto& p2) {
return *p1 < *p2;
} // C++ 14

  除了使用auto,还可以使用std::function类型来存储函数。其产生一个函数指针,但是可以指向任何可以像函数一样被调用的对象。形式如下:

std::function<bool(const std::unique_ptr<Widget>&, const std::unique_ptr<Widget>&)>
derefURLess = [](const std::unique_ptr<Widget>& p1, const std::unique_ptr<Widget>& p2) {
return *p1 < *p2;
}

  但是auto与std::function不同,auto仅仅存储闭包(closure),但是存储闭包的std::function模板的实例化,对于任何签名大小固定,当空间不够时会申请堆空间。结果是std::function相比auto使用更多的空间。

  以下是一个不使用auto而会产生隐式转换而造成错误的例子: 

std::unorder_map<std::string, int> m;
for(const std::pair<std::string, int>& p : m) {
...
} // 此处应为std::pair<const std::string, int>,所以会创建零时对象来绑定p

  使用auto也会因为表达式的特殊而产生未曾预料的结果。

总结

  • auto的变量必须被初始化,并且对因类型不符而引起的移植或效率问题免疫,并且可以简化重构过程,还可以减少输入
  • auto变量会遇到条款2和6提到的陷阱

  

  

[Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto的更多相关文章

  1. item 6: 当auto推导出一个不想要的类型时,使用显式类型初始化的语法

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 Item 5解释了比起显式指定类型,使用auto来 ...

  2. [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句

    条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...

  3. [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断

    条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...

  4. [Effective Modern C++] Item 3. Understand decltype - 了解decltype

    条款三 了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: ; // decltype(i) -> ...

  5. [Effective Modern C++] Item 4. Know how to view deduced types - 知道如何看待推断出的类型

    条款四 知道如何看待推断出的类型 基础知识 有三种方式可以知道类型推断的结果: IDE编辑器 编译器诊断 运行时输出 使用typeid()以及std::type_info::name可以获取变量的类型 ...

  6. [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  7. [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断

    条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...

  8. Effective Modern C++ Item 27:重载universal references

    假设有一个接收universal references的模板函数foo,定义如下: template<typename T> void foo(T&& t) { cout ...

  9. Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

随机推荐

  1. SPOJ QTREE 系列解题报告

    题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...

  2. (原)ubuntu上安装dlib

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5652791.html 参考网址: http://stackoverflow.com/questions ...

  3. 线程之一:JAVA线程基础

    参考core java,马士兵视频 1.线程的基本概念 (1)一个线程是一个程序内部的顺序控制流. (2)线程和进程 –每个进程都有独立的代码和数据空间(进程上下文),进程切换的开销大. –线程:轻量 ...

  4. mysql的client和sever之间通信password的传输方式

    本文想要说明的是,当我们用mysql -uroot -p1234567 -h127.0.0.1 -P3306 去连接mysql server时密码是通过什么样的形式传过去的呢? 首先密码这种东西明文传 ...

  5. [TYVJ] P1006 ISBN

    ISBN 背景 Background NOIP2008年普及组第一题   描述 Description    每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位 ...

  6. Unattended Setup Software Components (无人值守安装软件组件)

    原文 http://social.technet.microsoft.com/Forums/windows/en-US/d4ad85b4-8342-4401-83ed-45cefa814ec5/una ...

  7. UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>

    D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  8. Word Ladder II 解答

    Question Given two words (beginWord and endWord), and a dictionary's word list, find all shortest tr ...

  9. Binary Tree Zigzag Level Order Traversal 解答

    Question Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, fro ...

  10. Single Number 解答

    Question Given an array of integers, every element appears twice except for one. Find that single on ...