ADL是否不查找静态成员函数? [英] Doesn't ADL looks up static member functions?

查看:106
本文介绍了ADL是否不查找静态成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自跟进参数的跟进问题查找也只搜索名称空间或类吗?,@ DavidRodríguez在其中说: ADL会在类型的封闭名称空间中查找,并且还会在类型本身的内部内部。我可能误会他想说的话,但是我正在尝试以下示例:

This is follow up question from Does argument dependent lookup only search namespaces or classes too? , In which @David Rodríguez said "ADL will look in the enclosing namespace of the type, and also inside the type itself" . I may have got him wrong what he tried to say but I was trying this example:

struct foo{
    static void bar(foo* z){}    
};

int main(){
    foo* z;
    bar(z);
}

它不编译,产生错误未声明'bar'在这个范围内。是不是ADL不考虑静态成员函数?我的意思是在示例关联的类中是 foo ,所以ADL不会在类内部查看吗? 。有人可以在这里简化规则吗?

It doesn't compiles, producing the error " ‘bar’ was not declared in this scope " . Is it the case that ADL doesn't considers the static member function?. I mean in the example associated class is foo so wouldn't ADL look inside the class? . Can anyone please simplify the rules here?

推荐答案

他可能是这个意思:

struct foo{
    friend void bar(foo* z){}    //not static, its friend now
};

foo* z;
bar(z); //fine now

但是从技术上讲 bar()不是内部 foo 。它在 foo 的封闭命名空间中为 still

But then technically bar() is not inside foo. It is still in the enclosing namespace of foo.

-

编辑:

他确实是指 friend ,就像他说(强调我的意思):

He indeed meant friend, as he said (emphasis mine):


最好的例子是 friend 函数,该函数在类型中定义

The best example is a friend function that is defined inside the type

他的例子进一步说明了这一点。可能您需要阅读内部定义,而不仅仅是内部。

And his example illustrates further. Probably you need to read "defined inside", rather than only "inside".

define一词的不同之处在于,它看起来像函数的名称 bar 被引入到类的作用域中,但实际上,名称 bar 被引入到封闭的名称空间 foo (请参见第3.3.1 / 3-4和第11.3 / 6节)。

The word "defined" is all that makes the difference, because it looks like the function's name bar is introduced into the scope of the class, but in actuality, the name bar is introduced into the enclosing namespace of foo (see §3.3.1/3-4 and §11.3/6).

下面是一个更好的示例:

Here is a better example:

namespace Demo
{
     struct foo
     {
       friend void bar(foo* z){}
     };
}

foo *z;
bar(z); //foo (type of z) is inside Demo, so is bar
        //(even though bar is defined inside foo!)

bar(NULL);    //error - NULL doesn't help ADL.
bar(nullptr); //error - nullptr doesn't help ADL.

bar(static<foo*>(NULL)); //ok - ADL

请注意,名称 bar ,即使已引入到命名空间 Demo 中,也已隐藏,因此不能可以从外部使用通常的名称查找:

Note that the name bar, even though is introduced into the namespace Demo, is hidden, and thus cannot be used from outside using usual name-lookup:

using namespace Demo; //brings ALL (visible) names from Demo to current scope

bar(NULL); //STILL error - means bar is invisible

或者,

Demo::bar(NULL);       //error - not found
Demo::foo::bar(NULL);  //error - not found

希望有帮助。

这篇关于ADL是否不查找静态成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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