Mordern Effective C++ --auto

5. 优先使用auto而非显示类型声明

在C++之中,使用auto关键字声明类型能够将程序员从输入繁琐的类型中解放出来,编译器会自动推导出变量的实际类型。程序员

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

使用auto关键字函数

template<typename It>
void dwim(It b, It e)
{
  while(b != e){
    auto currValue = *b;
      ...
  }
}

在C++14中,lambda函数的参数均可以使用auto来定义。code

auto derefLess =                            // C++14 comparison
  [](const auto& p1,                          // function for
     const auto& p2)                          // values pointed
  { return *p1 < *p2; };

使用auto生命类型还能够将咱们从类型截断的问题中解放出来:对象

std::vector<int> arrs;
auto size = arrs.size();

在C++中,unordered_map的key的类型是const类型的,因此即使采起以下方式遍历unordered_map容器,仍然会产生临时对象:get

std::unordered_map<std::string, int> m;
   ...

for (const std::pair<std::string, int>& p : m)
{
   ...                  // do something with p
}

可是借助auto,咱们不只使声明更加简洁,还避开了此问题:编译器

std::unordered_map<std::string, int> m;
   ...

for (const auto& p : m)
{
   ...                  // do something with p
}

6. 当auto推导出非预期类型时应当使用显示的类型初始化

在C++中,由于标准不容许返回对bit的引用,因此对于vector<bool>标准库进行了特化处理,其[]运算符返回的是std::vector<bool>::reference类型的临时对象。对临时对象的修改会被其同步到vector中,于是这样使用auto关键字是不合规的。同步

Widget w;
…
auto highPriority = features(w)[5];         // w是否是个高优先级的?
…
processWidget(w, highPriority);             // 配合优先级处理w

在这种状况下,咱们只需显示指出highPriority的类型为bool便可规避此问题。string

相关文章
相关标签/搜索