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

前端 未结 4 712
醉酒成梦
醉酒成梦 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:13

    In Koenig Lookup, if a function is called without specifying its namespace, then the name of a function is also searched in namespace(s) in which the type of the argument(s) is defined. That is why it is also known as Argument-Dependent name Lookup, in short simply ADL.

    It is because of Koenig Lookup, we can write this:

    std::cout << "Hello World!" << "\n";
    

    Otherwise, we would have to write:

    std::operator<<(std::operator<<(std::cout, "Hello World!"), "\n");
    

    which really is too much typing and the code looks really ugly!

    In other words, in the absence of Koenig Lookup, even a Hello World program looks complicated.

提交回复
热议问题