What is “Argument-Dependent Lookup” (aka ADL, or “Koenig Lookup”)?

前端 未结 4 615
醉酒成梦
醉酒成梦 2020-11-21 05:39

What are some good explanations on what argument dependent lookup is? Many people also call it Koenig Lookup as well.

Preferably I\'d like to know:

  • Why
4条回答
  •  别那么骄傲
    2020-11-21 06:05

    Not everything about it is good, in my opinion. People, including compiler vendors, have been insulting it because of its sometimes unfortunate behavior.

    ADL is responsible for a major overhaul of the for-range loop in C++11. To understand why ADL can sometimes have unintended effects, consider that not only the namespaces where the arguments are defined are considered, but also the arguments of template arguments of the arguments, of parameter types of function types / pointee types of pointer types of those arguments, and so on and forth.

    An example using boost

    std::vector> v;
    auto x = begin(v);
    

    This resulted in an ambiguity if the user uses the boost.range library, because both std::begin is found (by ADL using std::vector) and boost::begin is found (by ADL using boost::shared_ptr).

提交回复
热议问题