member function hiding free function
The logical reason is Consistency.
- Suppose as per the suggestion, compiler resolves
foo(42)
to::foo(int)
. - Now after sometime, if you change
X::foo()
toX::foo(int)
thenfoo(42)
will be resolved toX::foo(int)
. Which is not consistent.
That is the also the reason why derived class function hides base class function when there are similar names.
Such cases can be resolved in 2 ways;
(1) Give fully qualified name (e.g. ::foo(42)
)
(2) Use using
utility; e.g.
void bar()
{
using ::foo;
foo(42);
}
A name in an inner scope hides names in outer scopes. It doesn't matter if it is a function or something else, or if you are in a class or a namespace.
Only if the name lookup finds several functions with the same name will the overload resolution kick in to try to select the one that is the best match for the call.
Because the two identifiers are defined in different scopes, and overload resolution only concerns about functions in the same scope. Once the compiler finds that the class has a foo
, it stops climbing up to wider scopes (C++11 §3.4.1/1), so the free function foo
is hidden.
You need to use a qualified name to refer to the global foo
:
::foo(42);