相关疑难解决方法(0)

什么是"Argument-Dependent Lookup"(又名ADL或"Koenig Lookup")?

什么是关于什么参数依赖查找的好解释?很多人也称它为Koenig Lookup.

我最好知道:

  • 为什么这是好事?
  • 为什么这是一件坏事?
  • 它是如何工作的?

c++ c++-faq name-lookup argument-dependent-lookup

163
推荐指数
4
解决办法
3万
查看次数

用于模板的限定和非限定名称查找的不同行为

这段代码应该如何表现?如果我在函数中使用限定名称,它会调用泛型函数忽略我的重载call_read(); 如果我使用非限定名称,它首先调用重载然后调用泛型版本.有什么不同?这是海湾合作委员会的一个错误吗?

#include <iostream>

struct info1 {};
struct info2 {};

template<class T> void read(T& x)
{
   std::cout << "generic" << std::endl;
}

template<class T> void call_read(T& x)
{
   ::read(x); // if I replace ::read(x) with read(x) the overload is called
}

void read(info1& x)
{
   std::cout << "overload" << std::endl;
}

int main()
{
   info1 x;
   info2 y;
   call_read(x);
   call_read(y);
}
Run Code Online (Sandbox Code Playgroud)

我也注意到它对基本类型有所不同.请参阅下面的代码

#include <iostream>

typedef struct info1 {};
typedef struct info2 {};
typedef int …
Run Code Online (Sandbox Code Playgroud)

c++ lookup templates language-lawyer argument-dependent-lookup

14
推荐指数
2
解决办法
1285
查看次数