什么时候应用ADL? [英] When is ADL applied?

查看:73
本文介绍了什么时候应用ADL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有3个示例:

我。

typedef int foo;

namespace B
{
    struct S
    {
        operator int(){ return 24; }
    };
        int foo(B::S s){ return 0; }
}

int main()
{
    int t=foo(B::S()); //24, ADL does not apply
}

II。

namespace B
{
    struct S
    {
        operator int(){ return 24; }
    };
        int foo(B::S s){ return 0; }
}

int main()
{
    int t=foo(B::S()); //0, ADL applies
}

III。

namespace B
{
    struct S
    {
        operator int(){ return 24; }
    };
        int foo(B::S s){ return 0; }
}
int foo(B::S s){ return 12; }

int main()
{
    int t=foo(B::S()); //error: call of overloaded ‘foo(B::S)’ is ambiguous
                       //ADL applies
}

对我来说不清楚什么是ADL查找的实际条件将适用?

It is not clear for me what is the actual conditions to ADL lookup will be apply? I need in reference to standard described it.

推荐答案

这个标准段落阐明了,甚至有一个非常类似于你的第一个例子的例子。

This Standard paragraph clarifies, and even has an example very much like your first example.

3.4.1 / 3:

3.4.1/3:


查找未限定名称用作函数调用的 postfix-expression ,如3.4.2 [basic.lookup.argdep]中所述。 [注意:为了确定(在解析期间)表达式是否为函数调用的 postfix-expression ,通常使用名称查找规则。 3.4.2中的规则对表达式的句法解释没有影响。例如,

The lookup for an unqualified name used as the postfix-expression of a function call is described in 3.4.2 [basic.lookup.argdep]. [Note: For purposes of determining (during parsing) whether an expression is a postfix-expression for a function call, the usual name lookup rules apply. The rules in 3.4.2 have no effect on the syntactic interpretation of an expression. For example,



typedef int f;
namespace N {
  struct A {
    friend void f(A &);
    operator int();
    void g(A a) {
      int i = f(a);    // f is the typedef, not the friend
                       // function: equivalent to int(a)
    }
  };
}




因为表达式不是函数调用,参数相关的名称查找(3.4.2)不适用,并且未找到朋友函数 f 。 - 结束注释]

这篇关于什么时候应用ADL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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