Effective C++11

C++应该算是流行的编程语言中最复杂的(之一)。看看这篇 C++, Ruby, CoffeeScript: a visual comparison of language complexity 就知道 C++ 相对于 Ruby 和 CoffeeScript 有多么复杂了。关于 C++ 还有段子:When your hammer is C++, everything begins to look like a thumb.(Steve Haflich in comp.lang.lisp,January 1995)。

也许正是因为C++ 如此复杂,Scott Meyers 的《Effective C++》才会受到如此的推崇,几乎在所有 C++ 书籍的推荐名单上,这部专著都会位于前三名。甚至有人说 C++ 程序员可以分成两类,读过 Effective C++ 的和没读过的。可见 Effective C++ 的地位。

去年 10 月 ISO/IEC 发布了 C++11 语言标准,open standards 上可以找到标准草案 n3242 (PDF),(wiki 上显示最终标准是3290,不过鉴于标准文档动辄几百刀,况且几乎没有人会对着标准文档学习,大家还是看看草案就好了,相信差别不大),C++ 11 更像是一个全新的语言,学习 C++ 11 可以参考 Bjarne StroustrupC++0xFAQ ,不过还是很期待 Effective 系列,不知道大师 Scott Meyers 什么时候会再出 Effective C++ 11,4月份的时候 Scott MeyersC++ and Beyond 上发出了 Effective C++ 11 的初步想法(原文在这里),先睹为快吧:

  • Prefer auto to Explicit Type Declarations
  • Distinguish () and {} When Creating Objects
  • Remember that auto + { expr } == std::initializer_list
  • Prefer non-member begin/end to member versions
  • Declare std::thread Members Last in Classes
  • Be Wary of Default Capture Modes in Lambdas Escaping Member Functions
  • Prefer Emplacement to Insertion
  • Pass std::launch::async if Asynchronicity is Essential
  • Minimize use of Weak Atomics
  • Distinguish Rvalue References from Universal References
  • Assume that move operations are neither present nor cheap
  • Prefer Lambdas over Binders
  • Prefer Lambdas over Variadic Arguments to Threading Functions
  • Be Wary of Oversubscription
  • Apply std::forward when Passing Universal References
  • Prefer std::array to Built-in Arrays
  • Use std::make_shared Whenever Possible
  • Prefer Pass-by-Reference-to-const to Pass-by-Value for std::shared_ptrs
  • Pass by Value if You’ll Copy Your Parameter
  • Reserve noexcept for Functions with Wide Interfaces
  • For Copyable Types, View Move as an Optimization of Copy
  • Prefer enum classes to enums
  • Prefer nullptr to NULL and 0
  • Distinguish among std::enable_if, static_assert, and =delete