是否可以将全局函数带入具有成员函数的重载分辨率? [英] Is it possible to bring global function into the overload resolution with member function?

查看:142
本文介绍了是否可以将全局函数带入具有成员函数的重载分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是相应的问题,我想知道的是,是否可能将全局函数带入过载具有成员函数的分辨率?

Here is the corresponding question, what I want to know is, Is it possible to bring global function into the overload resolution with member function?

我尝试了两种方法,但两者都不起作用:

I tried in 2 ways, but both don't work:

void foo(double val) { cout << "double\n";}

class obj {
public:
  using ::foo; // (1) compile error: using-declaration for non-member at class scope
  void callFoo() { 
    using ::foo; // (2)will cause the global version always be called
    foo(6.4); 
    foo(0); 
  }
private:
  void foo(int val) {cout << "class member foo\n"; }
};


推荐答案

这是一个普通的无限制名称查找,在§3.4.1[basic.lookup.unqual]中指定:

This is plain old unqualified name lookup, specified in §3.4.1 [basic.lookup.unqual]:


1在3.4.1中列出的所有情况下,按照每个类别中列出的顺序搜索范围为
的声明;
名称查找一结束就结束如果没有发现
声明,程序就会失败。

1 In all the cases listed in 3.4.1, the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name. If no declaration is found, the program is ill-formed.

8对于类<$ c的成员$ c> X 是在成员函数
body中使用的名称,在默认参数中,在异常规范非静态数据成员(9.2)的定义中,或者在定义X以外的类成员的定义中,
后面的成员的声明符-id ,应在
之一中通过以下方式声明:

8 For the members of a class X, a name used in a member function body, in a default argument, in an exception-specification, in the brace-or-equal-initializer of a non-static data member (9.2), or in the definition of a class member outside of the definition of X, following the member’s declarator-id, shall be declared in one of the following ways:


  • (6.3)中的成员,或

  • 应是 X 类的成员,或者是基本类 X (10.2)或

  • 如果 X Y (9.7)的嵌套类应是 Y 的成员,基本类 Y (此查找将
    依次应用于 Y 的封装类,或者

  • 如果 X 是一个本地类(9.8),或者是一个嵌套类局部类,在包含类 X 的定义
    的块中定义 X

  • 如果 X 是命名空间 N 的成员,属于 N 成员的类的类,或者是
    a中的本地类或嵌套类。函数的局部类的一个类,它是 N 之前使用
    名称,在命名空间 N

  • before its use in the block in which it is used or in an enclosing block (6.3), or
  • shall be a member of class X or be a member of a base class of X (10.2), or
  • if X is a nested class of class Y (9.7), shall be a member of Y, or shall be a member of a base class of Y (this lookup applies in turn to Y’s enclosing classes, starting with the innermost enclosing class), or
  • if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or
  • if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the use of the name, in namespace N or in one of N’s enclosing namespaces.

请注意,名称查找停止一旦找到声明。因此,如果你在 callFoo()中使用 use :: foo; ,查找 foo 将结束那里,从不触摸第二个点;如果你没有它,查找 foo 将在第二个项目符号处找到成员 foo()从不在别处搜索。指定未限定名称查找的方式意味着您将找到类成员或非类成员,但不能同时指定两个。

Note first that name lookup stops as soon as a declaration is found. So if you have using ::foo; in callFoo(), lookup for foo will end there and never touch the second bullet point; if you don't have it, lookup for foo will find the member foo() at the second bullet point and never search elsewhere. The way unqualified name lookup is specified means that you will either find class members or non-class-members, but never both.

这也在第13.1.1.1节中注明[over.call.func] / p3:

This is also noted in §13.1.1.1 [over.call.func]/p3:


在非限定函数调用中,名称不能通过 - >或。
运算符,并且具有更常用的 primary-expression 形式。
名称在函数调用的上下文中在函数调用(3.4)中查找名称查找的
正则规则。该查找找到的函数
声明构成候选
函数的集合。由于名称查找的规则,候选
函数的集合包括(1)完全非成员函数或(2)
完全是某些类的成员函数 T 。在情况(1)中,参数列表
与调用中的 expression-list 相同。在情况(2)中,
参数列表是由
增加的调用中的 expression-list ,如在限定函数中一样的隐含对象参数
调用

In unqualified function calls, the name is not qualified by an -> or . operator and has the more general form of a primary-expression. The name is looked up in the context of the function call following the normal rules for name lookup in function calls (3.4). The function declarations found by that lookup constitute the set of candidate functions. Because of the rules for name lookup, the set of candidate functions consists (1) entirely of non-member functions or (2) entirely of member functions of some class T. In case (1), the argument list is the same as the expression-list in the call. In case (2), the argument list is the expression-list in the call augmented by the addition of an implied object argument as in a qualified function call.






使用声明必须命名一个基类成员(§7.3.3[namespace.udecl] / p3):


A using-declaration at class scope must name a base class member (§7.3.3 [namespace.udecl]/p3):


用作成员声明
nested-name-specifier 将命名正在定义的类的基类。

In a using-declaration used as a member-declaration, the nested-name-specifier shall name a base class of the class being defined.

这篇关于是否可以将全局函数带入具有成员函数的重载分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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