tags:

views:

67

answers:

3

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);
  }
}
+1  A: 

Yes. Name lookup attempts to find all names visible at the point of usage. Once the entire superset of declarations of the name is built up, overload resolution is done in case the name is determined to be the name of a function. The name lookup ends if a matching name is found else fails.

If name lookup succeeds and if the name is determined to the name of a function, overload resolution is done to find the best viable function if any. Subsequently acess check is done to find out if the name is indeed accessible in the context of the access.

Note that visibility of the name is important for name lookup and not accessibility.

Chubsdad
@chubsdad:- When does accessibility comes in picture after overload resolution or before that.say I have same name one as private and one as public.name look up finds names now which one will happen overload or accessibility check??
Sudhendu Sharma
@Sudhendu Sharma: After name lookup and overload resolution. Once the best viable function is found, accessibility check is done to see if the best viable function is accessible at the point of access
Chubsdad
@chubsdad :- thanks for the nice explanation.
Sudhendu Sharma
+4  A: 

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.

Martin York
A: 

It is merely a way to speed up compilation, since it allows the compiler to stop searching much earlier.

Basically, the search is done in a scope by scope basis, beginning with the inner scopes and ending with the outer scopes. The fact that inner objects hide outer objects is referred to as shadowing.

This is a bit complicated by the presence of another rule: ADL (Argument Dependent Lookup) which adds to the current scopes the scopes from which the arguments come from (apart from built-in).

It makes for a very complicated logic. Also there are ways to trump ADL...

So, a few examples:

Inner Scope prevail

void func(int i);

namespace N
{
  void func(double d);

  void dummy() { func(1); } // calls N::func
}

struct T
{
  void func(char c);
  void func(double d);

  void dummy() { func(1); } // calls T::func (one of them, maybe ambiguity)
};

In both cases, it picks up the closer name.

With ADL

namespace N
{
  struct T;
}

std::ostream& operator<<(std::ostream& out, N::T t);

namespace N
{
  struct Custom;
  std::ostream& operator<<(std::ostream& out, Custom c);
}

void funky()
{
  N::T t;
  std::cout << t << '\n'; // compilation fails,
                          // no suitable operator<< in either std:: or N::
}

Here the problem is that the list of functions considered does not contain the right operator because it hasn't been declared in one of the namespaces of its operands.

Mixing ADL and class

namespace N
{
  struct T
  {
    T operator+(T const& rhs); // by the way, don't define +, -, etc... here ;)
  };

  T operator+(T const& lhs, T const& rhs);
}

N::T operator+(T const& lhs, T const& rhs);

Here we pick N::T::operator+(N::T const&) because it's member functions are preferred to free functions (most inner scope rule).

Matthieu M.
just another alias for above explanation :-Argument-dependent name lookup is also known as Koenig lookup.
Sudhendu Sharma
@Sudhendu: thanks :)
Matthieu M.