Things to Remember Item 8 • Prefer nullptr to 0 and NULL. • Avoid overloading on integral and pointer types. Item 9: Prefer alias declarations to typedef s • typedefs don’t support templatization, but alias declarations do. • Alias templates avoid the “::type” suffix and, in templates, the “typename” prefix often required to refer to typedefs. • C++14 offers alias templates ...
本章着重解释了auto在何时应该被使用,在何时不应该被使用; auto的好处是: 1. 变量初始化不会遗忘; 2. 定义变量更加简单; 3. 有时还可以避免因为错误的类型定义导致的隐式转换带来的性能问题; 例如: 在我自己的机器上, std::unordered_map a; for (const auto& p : a) { // Do something.... } 比 for (const std::pair& p : a) { // Do something .... } Benchmar...
auto a = {1, 2, 3}; 类型推断a为std::initializer_list。 auto b{1}; 类型推断b 为int。 template void templated_fn(T) {} templated_fn({1, 2, 3}); // compiler error! "{1, 2, 3}" is not an expression, it has no type, and so T cannot be deduced 这个auto type deduction和template type deduction主要的区别; int x是一个左值,(x)也是一个左值,但是前者decltype是int,后者是int&;
Errata: http://www.aristeia.com/BookErrata/emc++-errata.html Item2 int x1 = 27; int x2(27); int x3 = { 27 }; int x4{ 27 }; auto x1 = 27; // type is int, value is 27 auto x2(27); // ditto auto x3 = { 27 }; // type is std::initializer_list,value is { 27 } auto x4{ 27 }; // ditto 。 Item 5: Prefer auto to explicit type declarations auto variables have their type deduced from their initializer...
Cynosure (我是一只橘)
2018-08-05 12:46:28 1人喜欢
meerkat
2020-02-15 16:42:11
meerkat
2020-02-14 11:27:51
Cynosure (我是一只橘)
2018-08-04 10:35:45
Nightash
2015-01-05 15:39:26
Nightash
2014-12-31 15:31:16
Nightash
2014-12-30 15:15:43