SFINAE and detecting if a C++ function object returns void.
- by Tom Swirly
I've read the various authorities on this, include Dewhurst and yet haven't managed to get anywhere with this seemingly simple question.
What I want to do is to call a C++ function object, (basically, anything you can call, a pure function or a class with ()), and return its value, if that is not void, or "true" otherwise.
#include <stdio.h>
struct Foo {
void operator()() {}
};
struct Bar {
bool operator()() { return false; }
};
Foo foo;
Bar bar;
bool baz() { return false; }
void bang() {}
const char* print(bool b) { printf(b ? "true, " : "false, "); }
template <typename Functor> bool magicCallFunction(Functor f) {
return true; // lots of template magic occurs here...
}
int main(int argc, char** argv) {
print(magicCallFunction(foo));
print(magicCallFunction(bar));
print(magicCallFunction(baz));
print(magicCallFunction(bang));
printf("\n");
}