微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 在没有限定条件的名称空间中调用函数

看一下boost :: polygon的源代码,我看到了以下主题的许多应用:
#include <iostream>

namespace B {

struct A {
  void foo() const { std::cout << "foo" << std::endl; }
};

void bar(const A &a) { a.foo(); }
void baz() { std::cout << "baz" << std::endl; }

}

int main(int argc,char **argv) {
  B::A a;
  bar(a);
  B::baz(); // simply calling baz() does not work

  return 0;
}

如果没有额外的资格,可以如何调用bar(a)?我原以为只有B :: bar(a)会编译.

函数在命名空间内没有参数时,不会发生这种情况.

解决方法

根据ISO C 14标准,§3.4.2:

When the postfix-expression in a function call is an unqualified-id,other namespaces not considered during the usual unqualified lookup may be searched,and in those namespaces,namespace-scope friend function or function template declarations not otherwise visible may be found. These modifications to the search depend on the types of the arguments (and for template template arguments,the namespace of the template argument).

和以下:

For each argument type T in the function call,there is a set of zero or more associated namespaces and a set of zero or more associated classes to be considered. The sets of namespaces and classes is determined entirely by the types of the function arguments..

If T is a class type (including unions),its associated classes are: the class itself; the class of which it is a member,if any; and its direct and indirect base classes. Its associated namespaces are the innermost enclosing namespaces of its associated classes.

实际上你甚至可以通过附上函数名来防止这种情况发生:

(bar)(a); // doens't compile

(B::bar)(a); // does compile

还要注意,这仅适用于最里面的命名空间,这意味着在以下情况下,您需要限定命名空间:

namespace B {
   namespace C {
     struct A {};
   }

   void bar(const C::A& a) { ... }
}

原文地址:https://www.jb51.cc/c/119282.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐