mercredi 16 février 2022

Calling undeclared function template from inside another function template

I am learning about templates in C++ and so trying out different examples. One such example whose output i am unable to understand is given below:

template<typename T> void func(T p) {
g<T>(p); //ERROR
g(p);    //NO ERROR?
}


int main()
{

}

When i try to compile the above code snippet, i get error saying:

prog.cc: In function 'void func(T)':
prog.cc:2:1: error: 'g' was not declared in this scope
    2 | g<T>(p); //ERROR
      | ^
prog.cc:2:4: error: expected primary-expression before '>' token
    2 | g<T>(p); //ERROR
      |    ^

My questions are:

  1. Why i am getting this error?
  2. Why i am getting the error only for the statement g<T>(p); and not for g(p);? I thought that writing g(p); is equivalent to g<T>(p); since due to template argument deduction the template parameter for g will be deduced to T.

Aucun commentaire:

Enregistrer un commentaire