SFINAE and detecting if a C++ function object returns void.
        Posted  
        
            by 
                Tom Swirly
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Tom Swirly
        
        
        
        Published on 2010-12-26T21:19:28Z
        Indexed on 
            2010/12/27
            1:54 UTC
        
        
        Read the original article
        Hit count: 244
        
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");
}
© Stack Overflow or respective owner