According to C++ standard :-
The name lookup rules apply uniformly to all names (including typedef-names (7.1.3), namespace-names (7.3), concept-names (14.9), concept-map-names (14.9.2), and class-names (9.1)) wherever the grammar allows such names in the context discussed by a particular rule.
Name lookup rules apply Before Overload resolution takes place?
There must be some reason behind that I do not understand.
Following example is from Book C++ in a Nutshell :-
void func(int i)
{
std::cout << "int: " << i << '\n';
}
namespace N
{
void func(double d)
{
std::cout << "double: " << std::showpoint << d << '\n';
}
void call_func( )
{
// Even though func(int) is a better match, the compiler finds
// N::func(double) first.
func(3);
}
}
If I am working in namespace N.
I just wrote a function called func()
then I write some code to call this new function. It would seem counter intuitive for it to choose a function from another namespace before it used the function I just wrote.
(ie. It uses overload resolution)
I write a function bob(double)
. It works perfectly as there is no conflict with a `bob(double) in the global namespace. I write my code and the application works perfectly.
A year later somebody writes a function bob(int)
in the global namespace and re-compiles the application. Suddenly with no change in my code. All my calls to bob(5)
have been redirected to the bob(int)
in the global namespace.
This is highly undesirable way for the application to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With