I am trying to use a macro which takes advantage of the preprocessor __FUNCTION__
to switch to calling a function by the same name as the macro caller, but in a different class.
(This is a simple mocking framework implemented without inheritance.)
#define FAKE(def) enabled(__FUNCTION__) ? def : _foo.__FUNCTION__()
The usage is like this:
#include <iostream>
struct Foo {
int bar() { return 42; }
};
#define FAKE(def) enabled() ? def : _foo.__FUNCTION__()
struct Fake {
Fake(Foo& foo) : _foo(foo) {}
Foo& _foo;
bool enabled(const char*) const { return true; } // for example
int bar() {
return FAKE(1337); // doesn't work
}
};
which gives the error:
error: '__FUNCTION__' cannot be used as a function
This seems to be due to the fact that __FUNCTION__
is not a macro but a string literal.
Is there an easy-ish workaround? I would prefer not to cache some mapping of strings to function pointers or something. I know C++ doesn't support reflection, but I was hoping the preprocessor could sort this out somehow.