templates and checking what class it is

Started by
22 comments, last by CpMan 19 years, 7 months ago
Basically, depending on the situation, it's probably best to use the solution that's closest to the top of the list:
1. Template specialization
2. Type traits
3. RTTI

RTTI should of course be used in situations where it's impossible to determine the type at compile time (polymorphic cases).
Advertisement
Quote:Original post by Fruny
BTW, Ra, there is no standard support for typeof (I hope that'll change), you probably meant typeid
...you're kidding me, right?

hmm, it looks like typeid is what I meant, but I've always used typeof.. didn't know it wasn't standard. *shrug*
Ra
Quote:Original post by Ra
hmm, it looks like typeid is what I meant, but I've always used typeof.. didn't know it wasn't standard. *shrug*

You could always use sizeof trick described in Modern C++ Design [wink]
Quote:Original post by Ra
hmm, it looks like typeid is what I meant, but I've always used typeof.. didn't know it wasn't standard. *shrug*


typeof is supported by g++ and returns an actual type toke, not a type_info object you can compare.

That is, you can write: typeof(3.5f) x = 3.5f;, but not typeof(3.5f) == typeof(3.5f) as it would amount to writint float == float.

Note that for the usage typeof(3.5f) x = 3.5f;, there is a proposal to allow auto x = 3.5f; to have that meaning. Thus my hopeful stance. [smile]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Note that for the usage typeof(3.5f) x = 3.5f;, there is a proposal to allow auto x = 3.5f; to have that meaning. Thus my hopeful stance. [smile]

Python programmers managed to infiltrate the C++ committee? [grin]
Quote:Original post by CProgrammer
I first thought Id end up writing the same code twice but


You don't have to write the entire function twice, you can just write an overloaded function to replace that single if statement.

Quote:Then have an overloaded function that both just call a third function which takes a pointer to the abstract class.


You're programming in C++, use a reference, not a pointer:

template<class T>void foo(T& arg){  /* stuff */  foo_helper(arg);  /* stuff */}template<class T>void foo_helper(T& arg){  /* Not CMyClass */}template<>void foo_helper(CMyClass& arg){  /* CMyClass */}


Along with any other arguments you might need to manipulate foo's local variables from foo_helper.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by CoffeeMug
Python programmers managed to infiltrate the C++ committee? [grin]


Not quite, it's still static typing. The type is fixed at compile-time and once it has been set, it won't change: x will always be a float you can't later turn it into a std::string. You just get implicit typing in addition to explicit typing. It's closer to Haskell or O'Caml than to Python.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Yeah, I realize it's all predetermined in compile time. There's really no way to hack C++ type system to do dynamic type changes. It's just that that example looked a bit Pythonish [smile]
Actually, Andrew Koenig is a Python fan and a member of the C++ committee. [grin]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hehe, interesting read, thanks. Did anyone else think that his sense of interior decoration style sucks after looking at the pictures? [smile]

This topic is closed to new replies.

Advertisement