views:

50

answers:

2

Hello, I have some template function and I want to call it using define in c++:

#define CONFIG(key, type, def) getValue<type>(key, def);

Of course, it won't work. Could I make something like this?

+1  A: 

It works fine:

template<typename T>
T getValue( int, int ) { return T(); }

#define CONFIG(key, type, def) getValue<type>(key, def);


int main()
{
    CONFIG(1, int, 2);
    return 0;
}
Kirill V. Lyadvinsky
I passed type as string :D Okay, thanks.
Ockonal
And could I use default values in defines? Like: #define CONFIG(key, type, def="") ...?
Ockonal
No -- in that case, you'd want to define the default in the template itself, assuming you have control of the code.
JohnMcG
And if not, write another template with the default in place, and have your macro reference that.
JohnMcG
A: 

Why not just write it out? If you truly don't want to have to type a bazillion (is that a number? I forget) you can write a script to handle it for you.

def makeGetValue( _return, _1, _2 ):
    print _return + " getValue( int " + _1 + ", int " + _2 + ");"

Or something similar.

wheaties