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

前端 未结 9 1743
猫巷女王i
猫巷女王i 2020-12-13 19:43

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         


        
相关标签:
9条回答
  • 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)
        // ...
    }
    
    0 讨论(0)
  • 2020-12-13 19:55

    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.

    0 讨论(0)
  • 2020-12-13 20:01

    Define it explicitly, e.g.:

    template <>
    ostream& operator << (ostream &out,Vector<int>& vec)
    {
    }
    
    0 讨论(0)
  • 2020-12-13 20:07

    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;
    
    0 讨论(0)
  • 2020-12-13 20:08

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 20:16

    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.

    0 讨论(0)
提交回复
热议问题