参数依赖查找只搜索命名空间或类吗? [英] Does argument dependent lookup only search namespaces or classes too?

查看:114
本文介绍了参数依赖查找只搜索命名空间或类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Josuttis的模板书,我一直在试图把我的头放在ADL。他说ADL通过查找命名空间和类中的名称来继承,与调用参数的类型相关。我只是试图看看它是如何工作查找类中的名称。我在下面举一个我的测试的例子。我看看它是如何在命名空间中查找名称。

Ive been reading the Josuttis template book, and Ive been trying to put my head around ADL. He says "ADL proceeds by looking up the name in namespaces and classes "assocaited with" the types of the call arguments". Im just trying to see how it works looking up the name in a class. I put an example of my test below. I see how it looks up the name in a namespace.

class bryan_ns {
  public:
  class bryan {
    public:
      enum E { e1 };
      static void bryan_test() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  };

  void f(bryan::E) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};

void f(int)
{
  std::cout << "::f(int) called\n";
}


int main()
{
  f(bryan_ns::bryan::e1); // calls ::f(int)
}

但是如果我将bryan_ns改为命名空间如下:

But if I change bryan_ns to a namespace like so:

namespace bryan_ns {
  public:
  class bryan {
    public:
      enum E { e1 };
      static void bryan_test() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
  };

  void f(bryan::E) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};

void f(int)
{
  std::cout << "::f(int) called\n";
}


int main()
{
  f(bryan_ns::bryan::e1); // calls bryan_ns::f(bryan::E)
}


推荐答案

ADL将查找类型的封闭命名空间,以及类型本身中的 。最好的例子是在类型中定义的一个friend函数:

ADL will look in the enclosing namespace of the type, and also inside the type itself. The best example is a friend function that is defined inside the type:

namespace X {
class test {
   friend void f( test ) { std::cout << "test" << std::endl; }
};
}
int main() {
   X::test t;
   f( t );
}

调用 f(t)将会找到 X :: f ,其声明只在 test 类型中可用。这是朋友函数声明的一个已知的特征:它们声明一个命名空间级函数,但仅在类型内部提供声明。验证此行为的简单测试:

The call to f(t) will find X::f whose declaration is only available inside the type test. This is a little known feature of friend function declarations: they declare a namespace level function, but provide the declaration only inside the type. A simple test to verify this behavior:

namespace X {
   class test {
      friend void f( test );
   };
   //void f( test );          // [1]
}
void X::f( X::test ) {}       // [2]
int main() {
   X::test t;
   f(t);                      // [3]
}

2]将触发编译错误,因为您只能定义已经声明的函数,并且[2]不在命名空间 X 该定义不适用于自我声明的目的(如果你在它所在的命名空间中定义一个函数,那么该定义也是一个声明,但在这种情况下不是这样)。如果我们取消注释[1],错误将消失。或者,如果我们评论[2],代码将编译指出对于[3]中的调用,ADL已经在类中找到了声明。

The definition in [2] will trigger a compilation error, as you can only define a function that has already been declared, and as [2] is outside of the namespace X that definition does not serve the purpose of self-declaration (if you define a function inside the namespace where it resides, then the definition is also a declaration, but not in this case). If we uncomment [1] the error would go away. Alternatively, if we comment [2], the code will compile indicating that for the call in [3], ADL has found the declaration inside the class.

这篇关于参数依赖查找只搜索命名空间或类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆