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:
- Why i am getting this error?
- Why i am getting the error only for the statement
g<T>(p);
and not forg(p);
? I thought that writingg(p);
is equivalent tog<T>(p);
since due to template argument deduction the template parameter forg
will be deduced toT
.
Aucun commentaire:
Enregistrer un commentaire