I have many variables that are named the same as elements in an engineering specification document so the string version of the name is also useful.
I find myself using a macro like this a lot:
#define MACRO(a) a, #a
Typical usage is:
void someFunction(int a, const char *name);
someFunction(MACRO(meaningfully_named_variable));
My question is threefold:
- Is there a better way of doing this?
- Is a similar macro available in Boost or other libraries?
- If not, how could I refine and rename this to make it clear and useful?
Edit I should have said that the above is a minimal example. The function might have other parameters and the named entity might be a data member or perhaps even a function itself.
Another extension I'm considering for C++ is a class NamedRef
that could receive the contents of the macro.
template <typename T>
struct NamedRef
{
NamedRef(T *t, const char *name) : t(t), name(name) { }
T *t;
const char *name;
};
template <typename T>
NamedRef<T> namedRef(T &t, const char *name)
{
return NamedRef<T>(&t, name);
}
#define WITH_NAME(a) a, #a
// more sophisticated usage example
void otherFunction(double, NamedRef<int>, bool);
otherFunction(0.0, namedRef(object.WITH_NAME(meaningful_member_name)), false);
WITH_NAME()
.BOOST_STRINGIZE(X)
.