Skip to main content
GameDev.net gamedev.net
🔒 Locked

#define to template conversion

Started by foofightr Apr 6, 2005 at 1:46 AM 5 replies 1.4k views
Original Post
foofightr
foofightr

#define PRINT(a) printf(#a)
So PRINT(hello) will be replaced by printf("hello"). Basic preprocessor stuff. Is there any such functionality for C++ templates?

tempate <class T>
class Whatever
{
    void Function ()
    {
        printf(T); // <---- ???
    }
};
Is there any way I can make this work?
silvermace
silvermace
i think you've misinterpreted the use of templates. the example you've provided would generate illogical code, eg: the compiler would generate the following code if you made a Whatever class specialised with the template parameter T = int would look like this:
Whatever<int>::Function() {  printf(int);}


look into C++'s variable argument lists (MSDN page)
Illco
Illco
Obfuscating code?
moeron
moeron
Quote:
Original post by Anonymous Poster
Perhaps I'm missing something, but how would either one be useful?


The same way printf or sprintf are useful. You can send as many or as few arguments in as you wish, and they can be of varying types. I believe only standard types work though (float,int, string, pointers, etc.)

hth
moe.ron
moe.ron
foofightr
foofightr
Quote:
Original post by silvermace
i think you've misinterpreted the use of templates. the example you've provided would generate illogical code, eg: the compiler would generate the following code if you made a Whatever class specialised with the template parameter T = int would look like this:
Whatever<int>::Function() {  printf(int);}


look into C++'s variable argument lists (MSDN page)


I was thinking more like printf("int"). I shouldn't have used printf as an example - this is not about variable argument lists, it's about simple string representation of the template argument. If only #T worked...

I was trying to find a way around template specialization, but I suppose I will need to have snippets like this all over the place:

template<>void Whatever<int>::Function (){    printf("int");}template<>void Whatever<float>::Function (){    printf("float");}etc...
_the_phantom_
_the_phantom_
errm... maybe typeid(exp).name() is what you need... Clicky 1 (slight non-standard useage with header files but should give you the idea), oh yeah, requires RTTI to be enabled as well

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.