Calling a Global Function Which Has the Same Name as a Member Function

Call global function with same name as class method?

You don't need to change the function name to use them.


As the comments say, you can fully qualify the function e.g.

void foo()
{
}

class Bar
{
void foo()
{
}

void wibble()
{
::foo();
}
};

:: means look in global scope

Calling a global function which has the same name as a member function

Chris Lattner suggests qualifying the name of the global function with Swift's default namespace, Swift. So you should be able to access the global version using: Swift.sort.

Why can't a class method call a global function with the same name?

The member function is hiding the global. It finds the name in the class context, therefore it not continue to search it in other contexts.

You need to call it like this:

::foo(1);

Another solution is to use forward declaration inside the function, like this:

void Bar::foo()
{
void foo(int);
foo(1);
}

As Praetorian suggests, here is another option:

void Bar::foo()
{
using ::foo;
foo(1);
}

Calling a global function which has the same name as a member function Part 2

I usually do that by importing the framework like you did in your commented code:

import Parsing

and then using the full name, including the namespace (module), again like you did:

Parsing.failure()

You should check that:

  • the entity you are trying to access to (in this case a function) is declared as public
  • the module name (Parsing) is correct
  • the function is actually a global function and not a class/struct method
  • target -> Build Settings -> Packaging -> Product Module Name is the actual model name you are trying to import

Last, if you change the name of the class func failure() method, does it work?

Why calling a non-member function with the same name as a member function generates an error


Why do I need to use :: to call the non-member function with the same name as the member function, but with different signature

Because those are the rules. Names in a nested scope hide entities with the same name in a wider scope.

What is the motivation for this requirement?

Consider the case where a member function calls another member with a signature that doesn't quite match:

struct A {
void f(double);
void g() {f(42);} // requires int->double conversion
};

Now suppose someone adds an unrelated function in the surrounding namespace

void f(int);

If this were included in the set of overloads within the scope of A, then suddenly the behaviour of A::g would change: it would call this instead of A::f. Restricting the overload set to names in the narrowest available scope prevents this kind of unexpected breakage.

As you (and your helpful compiler) say, the outer name is still available (with qualification) if you need it.

calling a global function from a local function with the same name in swift

Well, if you can help it, don't do the first thing. If you must, make your global functions static methods of a struct and you'll be able to reach them that way:

struct Logger {
static func look(){
//log stuff
}
}

class MyClass {
func look(){
Logger.look()
}
}

That is crazy that String in Swift isn't Printable. If you want to add it yourself, this will do it:

extension String: Printable {
public var description: String { return self }
}

In the mean time, time to file a radar!

C++: Why member function has priority over global function

Consider what would happen if a global function declared somewhere in your code base (possibly several #include statements away) would trump an class obj member function declared right there in the class itself...

It would mean that, if you want to play it safe, you would have to fully qualify every single call to a member function...

this->foo();

...instead of having to qualify the less likely case of actually referring to the global function.

::foo();

This is called the "concept of least surprise".

c++ member function hides global function


c++ member function hides global function

The problem is that for the call expression f(m) name lookup finds the member function N::f(int) and so the search/lookup stops. Now this found member function N::f(int) has a parameter of type int but we're passing an argument of type M and since there is no implicit conversion from M to int, this call fails.

To solve this, use the scope operator:: to tell the compiler that you want to call the global function f as shown below:

struct N {
M m;
void f(int i) {
//------vv---------->use the scope operator :: to call the global version
::f(m);
}
};

Working Demo

class static member function chosen over global function with same name?

Note: I think my earlier answer was wrong. Its not Koenig Lookup i.e Argument-dependent name lookup (ADL). So I deleted my (earlier) answer since I found the relevant section from the Standard which answers your question.

Your code is directly from the section §9.4/2 of the C++03 Standard.

A static member may be referred to
directly in the scope of its class or
in the scope of a class derived
(clause 10) from its class; in this
case, the static member is referred to
as if a qualified-id expression was
used, with the nested-name-specifier
of the qualified-id naming the class
scope from which the static member is
referenced.

It then gives this example (which you've asked in the question)

[Example:

int g();
struct X {
static int g();
};
struct Y : X {
static int i;
};
int Y::i = g(); // equivalent to Y::g();

—end example]

It then says that in §9.4/3

If an unqualified-id (5.1) is used in
the definition of a static member

following the member’s declarator-id,
and name lookup (3.4.1) finds that the
unqualified-id refers to a static
member, enumerator, or nested type of
the member’s class (or of a base class
of the member’s class), the
unqualified-id is transformed into a
qualified-id expression in which the
nested-name-specifier names the class
scope from which the member is
referenced
.

Since that happens only in the definition of the static member, that means Y::g() is called ONLY in the initialization, not in assignment:

//definition-cum-initialization
int Y::i = g(); // equivalent to Y::g();
int main()
{
//assignment
Y::i = g(); // does not equivalent to Y::g(); it calls global g()
}

See the output here : http://www.ideone.com/6KDMI

Lets consider another example:

struct B{};

B f();

namespace NS
{
struct A { static B b;};
B f();
}

//Definition cum Initialization
B NS::A::b = f(); //calls NS::f()
B b = f(); //calls global f()

int main()
{
//Assignment
NS::A::b = f(); //calls global f()
b = f(); //calls global f()
}

See the complete demo here : http://www.ideone.com/53hoW



Related Topics



Leave a reply



Submit