Meir
Meir

Reputation: 12775

how to query if(T==int) with template class

When I'm writing a function in a template class how can I find out what my T is?

e.g.

template <typename T>
ostream& operator << (ostream &out,Vector<T>& vec)
{
if (typename T == int)
}

How can I write the above if statement so it works?

Upvotes: 29

Views: 22760

Answers (9)

Khazratbek
Khazratbek

Reputation: 1656

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

Upvotes: 3

leemes
leemes

Reputation: 45705

Since C++11 we have std::is_same:

if (std::is_same<T, int>::value) ...

It's implemented similar to the suggested trait TypeIsInt suggested in the other answers, but with two types to be compared.

Upvotes: 26

anon
anon

Reputation:

The easiest way is to provide a template specialisation:

#include <iostream>
#include <vector>
using namespace std;

template <typename T> struct A {
};

template <typename T > 
ostream & operator <<( ostream & os, A<T> & a  ) {
    return os << "not an int" << endl;
}


template <> 
ostream & operator <<( ostream & os, A<int> & a  ) {
    return os << "an int" << endl;
}

int main() {
    A <double> ad;
    cout << ad;
    A <int> ai;
    cout << ai;
}

Upvotes: 9

avakar
avakar

Reputation: 32635

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.

Upvotes: 8

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248129

Simplest, most general solution: Just write a plain old overload of the function:

ostream& operator << (ostream &out,Vector<int>& vec)
{
// Your int-specific implementation goes here
}

This assumes that the int and non-int versions don't have much code in common, as you have to write two separate implementations.

IF you want to use one common implementation of the function, with just an if statement inside that differs, use Charles Bailey's implementation:

template< class T >
struct TypeIsInt
{
    static const bool value = false;
};

template<>
struct TypeIsInt< int >
{
    static const bool value = true;
};

template <typename T>
ostream& operator << (ostream &out,Vector<T>& vec)
{
    if (TypeIsInt< T >::value) {
      // your int-specific code here
    }
}

In general, don't use typeid if you don't need to.

Upvotes: 10

siddhant3s
siddhant3s

Reputation: 420

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

Upvotes: 6

Serge
Serge

Reputation: 7694

C++ templates don't work this way. The general idea of templates is express somethings which is common for a lot of different types. And in your case you should use template specialization.

template<class T> ostream& operator<< (ostream& out, const vector<T>& v)
{
    // your general code for all type
}
// specialized template
template<> ostream& operator<< <int>(ostream& out, const vector<int>& vec)
{
    // your specific to iny type code goes here
}

Then C++ compiler will call this function when you use int type and general implementation for any other type

std::vector<int> f(5, 5);
std::cout << f;

Upvotes: 0

Artem Barger
Artem Barger

Reputation: 41232

Define it explicitly, e.g.:

template <>
ostream& operator << (ostream &out,Vector<int>& vec)
{
}

Upvotes: 11

CB Bailey
CB Bailey

Reputation: 792497

Something like this:

template< class T >
struct TypeIsInt
{
    static const bool value = false;
};

template<>
struct TypeIsInt< int >
{
    static const bool value = true;
};

template <typename T>
ostream& operator << (ostream &out,Vector<T>& vec)
{
    if (TypeIsInt< T >::value)
    // ...
}

Upvotes: 42

Related Questions