ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c普通查找与参数依赖查找

2019-07-27 07:06:09  阅读:188  来源: 互联网

标签:name-lookup c argument-dependent-lookup


考虑http://en.cppreference.com/w/cpp/language/adl中描述的此示例:

namespace A {
      struct X;
      struct Y;
      void f(int);
      void g(X);
}

namespace B {
    void f(int i) {
        f(i);   // calls B::f (endless recursion)
    }
    void g(A::X x) {
        g(x);   // Error: ambiguous between B::g (ordinary lookup)
                //        and A::g (argument-dependent lookup)
    }
    void h(A::Y y) {
        h(y);   // calls B::h (endless recursion): ADL examines the A namespace
                // but finds no A::h, so only B::h from ordinary lookup is used
    }
}

我想知道为什么出现歧义,因为如果不考虑ADL规则

“the lookup set produced by usual unqualified lookup contains any of the following”.

根据规则,这里可以通过非限定查找找到B :: g,如http://en.cppreference.com/w/cpp/language/unqualified_lookup中所述

For a name used in the definition of a function, either in its body or as part of default argument, where the function is a member of user-declared or global namespace, the block in which the name is used is searched before the use of the name, then the enclosing block is searched before the start of that block, etc, until reaching the block that is the function body. Then the namespace in which the function is declared is searched until the definition (not necessarily the declaration) of the function that uses the name, then the enclosing namespaces, etc.

那么我的问题是为什么在这种情况下考虑ADL规则?

解决方法:

完整的报价是

First, the argument-dependent lookup is not considered if the lookup set produced by usual unqualified lookup contains any of the following:

  1. a declaration of a class member
  2. a declaration of a function at block scope (that’s not a using-declaration)
  3. any declaration that is not a function or a function template (e.g. a function object or another variable whose name conflicts with the name of the function that’s being looked up)

这意味着只有当非限定查找产生上述三个结果之一时,才会忽略ADL.由于我们没有处理类成员,因此该函数在命名空间范围内声明,而不是块作用域,我们只查找继续使用的函数并使用ADL.

标签:name-lookup,c,argument-dependent-lookup
来源: https://codeday.me/bug/20190727/1551473.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有