0

I am trying the below simple thing to exercise the behavior of ADL

 namespace test{
   struct S{ public: s():a(10){} int a;};
   template<typename T>
   void fun(S o){
       T a{};
       std::cout<<(a+o.a)<<"\n";
   }
 }

 int main(){
     test::S  A;
     fun<int>(A);
 }
    error1: expected primary-expression before ‘int’
       fun<(int)>(A);
    error2:'fun’ was not declared in this scope
       fun<int>(A);

I am not sure about the first error(has it anything to deal with C++ vexing parse?) and for the second error since I've passed the argument of type S I believe that the ADL will consider S and its associated namespace test but not sure why I am getting 'fun’ was not declared error.

RaGa__M
  • 2,550
  • 1
  • 23
  • 44

1 Answers1

-1

You placed fun<> in the test-namespace.

So call it with test::fun

Tomas Dittmann
  • 424
  • 7
  • 18
  • 2
    Good attempt, but I think you missed the point of the question. There is a feature in C++ called ADL which would render this unnecessary, and the question is why ADL is not being triggered in the given code. – Lightness Races in Orbit Oct 09 '16 at 18:49
  • @Tomas Dittmann i appreciate your answer but i have came across this "ADL proceeds by looking up the name in namespaces and classes "associated with" the types of the call arguments.The precise definition of these associated namespaces and associated classes is given later, but intuitively they can be thought as being all the namespaces and classes that are fairly directly connected to a given type." – RaGa__M Oct 09 '16 at 18:50
  • Ahh I've never heard of ADL. Just reading about it right now. Learning by giving false answers... – Tomas Dittmann Oct 09 '16 at 18:52
  • @TomasDittmann well it happens :) – RaGa__M Oct 09 '16 at 18:53