|
-
Apr 1st, 2002, 12:57 PM
#1
Thread Starter
Frenzied Member
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(T &a, T &b)
{
if int or long
a = 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

-
Apr 1st, 2002, 01:07 PM
#2
Monday Morning Lunatic
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
-
Apr 1st, 2002, 02:06 PM
#3
transcendental analytic
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.
-
Apr 2nd, 2002, 01:50 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|