Reputation: 161
I'm writing a helper function that is supposed to make it simpler and more foolproof for some (C/C++ newbie) colleagues of mine to retrieve named, scalar parameter values from a parameter store.
The thing is, the parameter store can only store values of type double
, but the code which will be calling this function is a big messy pile of C that was converted to C++, so there are places where it can cause problems (or at least spurious warnings) to just dump a double
in where e.g. an int
or long
is expected.
So I had the idea of making the helper function a template function with the return type being an unspecified template argument - that way, the caller has to manually specify what the return type should be.
However, the argument to the function is a unicode string (const wchar_t*
), and I wanted the users to be able to call it like they used to do with symbolic names (done using macros, previously)..
I can't figure out how to combine the template function thing with a way to automatically stringify the argument! Can anyone offer some guidance? I'm basically looking for a clever macro/template hack I guess, for aesthetic reasons ;-)
As a macro:
// the return type is always double
#define GetParameter(parameterName) GetParameterFromParameterStore(L#parameterName)
As a template function:
// the user has to remember to pass the argument as a (wide) string
template<class T> T GetParameter(const wchar_t* parameterName)
{
return (T)GetParameterFromParameterStore(parameterName);
}
Edit: Ideally, I would like to able to call the function like:
int _volumePct = GetParameter<int>(VolumeInPercent);
(without any extra decoration or syntax).
Upvotes: 0
Views: 866
Reputation: 311
One way is to make a new macro to stringify
#define Stringify(parameter) L#parameter
and to pass it to GetParameter template function as below:
GetParameter<int>(Stringify(hello there));
Is this what you are trying to do? but then, I feel it is better to just type-cast the result using the existing macro.
Upvotes: 1