Is there a way to tell a template TYPE in its function?

This is a discussion on Is there a way to tell a template TYPE in its function? within the C++ Programming forums, part of the General Programming Boards category; The code should not compile, but is there a way to tell if T is an "int" type in foo()? ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Is there a way to tell a template TYPE in its function?

    The code should not compile, but is there a way to tell if T is an "int" type in foo()?

    Code:
    template <typename T>                                                           
    class A{                                                                        
    public:                                                                         
        void foo();                                                                 
    };                                                                              
                                                                                    
    template <typename T>                                                           
    void foo<T>()                                                                   
    {                                                                               
        if (T=="int")                                                               
            cout<<"int"<<endl;                                                      
    }                                                                               
                                                                                    
    int main(int argc, char* argv[]){                                               
        A<int> a;                                                                   
        a.foo();                                                                    
        return 0;                                                                   
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Did you try using the typeid operator?
    I never use it because I found it to be unreliable, but maybe for primative types it would be OK?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    it's called 'template specialization'. so basically:

    Code:
    template <typename T>                                                           
    class A{                                                                        
    public:                                                                         
        void foo();                                                                 
    };                                                                              
                                                                                    
    template <>                                                           
    class A<int>{                                                                        
    public:                                                                         
        void foo();                                                                 
    };      
    
    template <typename T>                                                           
    void A<T>::foo()                                                                   
    {                                                                               
            cout<<"A<T>::foo()"<<endl;                                                      
    }   
                                                              
    void A<int>::foo()                                                                   
    {                                                                               
            cout<<"A<int>::foo()"<<endl;                                                      
    }                                                                              
    
    int main(int argc, char* argv[]){                                               
        
        A<double> a;                                                                   
        A<int> b; 
        a.foo(); 
        b.foo();                                                                   
        return 0;                                                                   
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    Use traits if you need part of the behavior of a template to be specific for various types. (In practice it is better to factor the type specific parts out into their own function.)

    And you don't need to specialize an entire template to specialize a single function.

    Soma

    Code:
    #include <iostream>
    
    template <typename T>
    struct is_int
    {
    operator bool(){return false;}
    };
    
    template <>
    struct is_int<int>
    {
    operator bool(){return true;}
    };
    
    template <typename T>                                                           
    class A{                                                                        
    public:                                                                         
        void foo();                                                                 
    };                                                                              
                                                                                    
    template <typename T>                                                           
    void A<T>::foo()                                                                   
    {                   
    if(is_int<T>())
    {
    std::cout<<"int";
    }                
    else
    {
    std::cout<<"not int";
    }                                                                                            
    }       
    
    template <>                                                           
    void A<double>::foo()                                                                   
    {          
    std::cout<<"double";                                                                                                                     
    }                                                                               
                                                                                    
    int main(int argc, char* argv[]){                                               
        A<int> a;                                                                   
        a.foo();      
        A<double> b;
        b.foo();
        A<long> c;
        c.foo();                                                         
        return 0;                                                                   
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can't do that if you want to use type-specific code (i.e. something that only compiles for one type) in that template.

    Just specialize the function.
    Code:
    template <typename T>                                                           
    class A{                                                                        
    public:                                                                         
        void foo();                                                                 
    };                                                                              
                                                                                    
    template <typename T>                                                           
    void A<T>::foo()                                                                   
    {                                                                               
    }                                                                               
    
    template <>
    void A<int>::foo()
    {
            cout<<"int"<<endl;
    }
                                                                                    
    int main(int argc, char* argv[]){                                               
        A<int> a;                                                                   
        a.foo();                                                                    
        return 0;                                                                   
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21