Template function with namespace expansion becomes visible in another namespace

I went through strange behavior of the namespace scope (using g ++ 4.8.2). To explain the problem, I extracted the minimal code that reproduces this strange behavior:

namespace Weird {
  template <typename T>
    struct Rec
    {
      T val;
      Rec( T const& _val ) : val( _val ) {}
    };

  template <typename T>
    Rec<T>
    foo( Rec<T> const& r )
    {
      return Rec<T>( r.val * 2 );
    }

};

Weird::Rec<double>
bar( Weird::Rec<double> const& _input )
{
  return foo( _input );
}

      

In this code, I would expect g ++ to complain that "foo" is not defined in the scope of "bar", which is not the case; the code just compiles.

So, I am a little confused. Is g ++ (namespace leak) correct? Or, if not, by what mechanism does "foo" become visible in the "bar"?

+3


source to share


1 answer


This Argument Dependent Search (ADL), also known as Koenig search.



In short, using an operator or calling an unaddressed function will find names in the enclosing namespaces of the operands or arguments.

+9


source







All Articles