依赖于ADL for std :: begin()和std :: end()?

Sta*_*ked 9 c++

迭代标准容器时,您认为省略std::前缀并依赖ADL查找定义是个好主意吗?例:

std::vector<int> vec = get_vec();
// range-based for loop would be preferred here, but just for the sake of example
for (auto it = begin(vec), end = end(vec); it != end; ++it) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

有没有理由这样做?

R. *_*des 16

如果您打算使用ADL来更改容器类型而不更改循环,则添加using std::begin; using std::end;.这确保它std从具有beginend成员的其他名称空间中找到容器的函数,但在其名称空间中没有自由函数.

namespace my {
    template <typename T>
    struct container {
        // ... stuff
        iterator begin();
        iterator end();
    };
    // no begin/end free functions
}


my::container<int> vec = get_vec();
using std::begin;
using std::end;
for (auto it = begin(vec), end = end(vec); it != end; ++it) { /*...*/ }
// defaults to std::begin which defaults to .begin() member function
Run Code Online (Sandbox Code Playgroud)


Naw*_*waz 8

你认为省略std ::前缀并依靠ADL查找定义是个好主意吗?

我认为这是个好主意.在这样的模板中有必要:

template<typename Container>
void do_work(Container const & c)
{ 
  using std::begin;  //enable ADL
  using std::end;    //enable ADL 

  //now let compiler search the correct begin/end in the initialization part
  for(auto it = begin(c), itend = end(c); it != itend ; ++it)
  {
       //do work
  } 
}
Run Code Online (Sandbox Code Playgroud)

这里既然Container可以是程序员定义的类型,比如在命名空间中xyz,那么如果我写的std::begin不是begin(在初始化部分中),上面的函数模板将如何工作?