Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3.4.1 Unqualified name lookup

Tags:

c++

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);
  }
}
like image 706
Sudhendu Sharma Avatar asked Dec 28 '22 08:12

Sudhendu Sharma


1 Answers

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.

Now consider the situation is reversed.

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

like image 89
Martin York Avatar answered Jan 11 '23 03:01

Martin York