why i am getting "expected primary-expression before '<<' token|" while printing the datatype passed into the template

#include <iostream>
using namespace std ;
template <typename T>

void function_(T val )
{
//only this line works file
cout<<"value is : "<<val<<endl;
//after adding this line compiler points the error
cout<<"and the data type is : "<<T<<endl;
}

int main(void)
{
function_<int>(5);
return 0;
}

Last edited on
Because T is a type and you cannot pass a type as argument to an operator.
With a template function, the T gets replaced with the type that is actually used.
So this:
cout<<"and the data type is : "<<T<<endl;
becomes (if you use an int for the type)
cout<<"and the data type is : "<< int <<endl;
or (if you use a double for the type)
cout<<"and the data type is : "<< double <<endl;
or (if you use a string for the type)
cout<<"and the data type is : "<< string <<endl;

See how it makes no sense?
Topic archived. No new replies allowed.