When I\'m writing a function in a template class how can I find out what my T is?
e.g.
template
ostream& operator << (os
TypeID is never a good idea. It relies on RTTI. By the way here is your answer :http://www.parashift.com/c++-faq-lite/templates.html#faq-35.7
One more solution is:
if(std::is_same<T, int>::value)
//It is int
if (std::is_same<T, double>::value)
//It is double
if (std::is_same<T, long double>::value)
//It is long double
This way.
ostream & operator << (ostream &out, Vector<int> const & vec)
{
// ...
}
The compiler will choose this function over the function template if you pass Vector<int>
.
Edit: I found this article, which attempts to explain why to prefer overloading to template specialization.