Jesper Schmidt
Jesper Schmidt

Reputation: 31

C++ using declaration and argument dependent lookup

Is the code below valid C++98 or does it require a newer version of the C++ standard?

namespace basic
{
  void f(int) {}
}

namespace lib
{
  template<class T1, class T2>
  void g(T1 x1, T2 x2)
  {
    using basic::f; // pull in function f for basic types without ADL
    f(x1);
    f(x2); // error: no suitable conversion function from "user::c" to "int" exists
  }
}

namespace user
{
  class c {};

  void f(c) {}
}

int main()
{
  lib::g(1, user::c());
}

Apparently, my compiler (based on EDG front end, I guess) does not consider user::f after the using declaration of basic::f. Is this correct behavior according to C++98?

Upvotes: 3

Views: 215

Answers (1)

dascandy
dascandy

Reputation: 7302

I think that it's not correct behaviour of your compiler. Your template should be only actually resolved / instantiated after the point of declaration of use. So at that point it should resolve the names for that instantiation, and those include the ADL-pulled in lookup, so that should have worked.

I think MSVC still has a pending thing to do this two-phase lookup though, and your compiler (EDG frontend? What's the name of the compiler) may also do that. I know for a fact that MSVC2015 does not do a proper two-phase lookup, but I don't know for sure if that applies to this example, and your implication is that you're not using that compiler.

Upvotes: 2

Related Questions