Why does gcc hide overloaded functions in the global namespace? Is this conforming to the C++ standards?
Yes. In short, you can't overload functions through different scopes. According to the rule of unqualified name lookup, for the invoking of f()
in g()
, the name f
could be found inside the namespace test
, then the name lookup stops; overload resolution takes place after that (based on the names found). That means f()
in global namespace won't be considered at all even it looks more appropriate here.
(emphasis mine)
For an unqualified name, that is a name that does not appear to the
right of a scope resolution operator ::, name lookup examines the
scopes as described below, until it finds at least one declaration
of any kind, at which time the lookup stops and no further scopes are
examined.
In order to compile a function call, the compiler must first perform
name lookup, which, for functions, may involve argument-dependent
lookup, and for function templates may be followed by template
argument deduction. If these steps produce more than one candidate
function, then overload resolution is performed to select the function
that will actually be called.
You can use using
to introduce the names into the same scope, i.e. to make them actual overloaded functions.
namespace test
{
using ::f; // introduce the name from global namespace
void f(int) {}
void g() { f(); } // fine
}