Results 1 to 4 of 4

Thread: Template question

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    Template question

    Is there a way to tell what type of VAR comes in through a template function?

    like this for example
    PHP Code:
    template <typename T>
    void Combine(&a&b)  
    {
      if 
    int or long
        a 
    b;
      if 
    string
        a 
    += b;
      if 
    char
       strcpy
    (a,b);
      
    etc..

    I though I saw somewhere how to do this, but I can't find it.

    Thanks
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    typeid might help you here...

    But for this, you're best specialising:
    Code:
    template<typename T>
    void Combine(T &target, const T &ref) {
        target += ref;
    }
    
    Combine<char*>(char *&target, const char *&ref) {
        strcat(target, ref);
    }
    ...or something.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the template argument T you are using is the type
    If you don't want to implement functionality within T you can call a functor and partial specialize for each T you desire, ex:

    template<typename T>
    struct ftr{
    static inline char* name(){return "no specific T";}
    };
    template<>
    struct ftr<int>{
    static inline char* name(){return "T is int";}
    };
    etc...

    template<typename T>
    void Combine(T &target, const T &ref) {
    ...
    ftr<T>::name
    ...
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    typeid works only for classes with virtual functions when RTTI is enabled.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width