Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doesn't ADL looks up static member functions?

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);
}

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?

like image 823
M3taSpl0it Avatar asked Feb 06 '13 09:02

M3taSpl0it


People also ask

Are static functions member functions?

Static Function: It is a member function that is used to access only static data members. It cannot access non-static data members not even call non-static member functions.

What limitations does a static member function have?

What limitation does a static member function have? Static member functions can only access member variables that are also static.

What are the restrictions on the use of static member functions?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .

Can a member function be static?

Static Function Members A static member function can only access static data member, other static member functions and any other functions from outside the class. Static member functions have a class scope and they do not have access to the this pointer of the class.


1 Answers

He probably meant this:

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

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

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

--

EDIT:

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

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".

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

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

Or,

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

Hope that helps.

like image 99
Nawaz Avatar answered Oct 13 '22 01:10

Nawaz