Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3608194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:28:16+00:00 2026-05-18T21:28:16+00:00

I’ve read the various authorities on this, include Dewhurst and yet haven’t managed to

  • 0

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.

using std:

struct Foo {
  void operator()() { cout << "Foo/"l; }
};
struct Bar {
  bool operator()() { cout << "Bar/"; return true; }
};

Foo foo;
Bar bar;
bool baz() { cout << "baz/"; return true; }
void bang() { cout << "bang/"; }

const char* print(bool b) { cout << b ? "true/" : "false/"; }

template <typename Functor> bool magicCallFunction(Functor f) {
  return true;  // Lots of template magic occurs here 
                // that results in the functor being called.
}

int main(int argc, char** argv) {
  print(magicCallFunction(foo));
  print(magicCallFunction(bar));
  print(magicCallFunction(baz));
  print(magicCallFunction(bang));
  printf("\n");
}
// Results:  Foo/true/Bar/true/baz/true/bang/true

UPDATE

Thanks for the thoughts and ideas!

Based on this, I actually decided to bring up all my templating one level – so instead I have:

bool eval(bool (*f)()) { return (*f)(); }

bool eval(void (*f)()) { (*f)(); return true; }

template <typename Type>
bool eval(Type* obj, bool (Type::*method)()) { return (obj->*method)(); }

template <typename Type>
bool eval(Type* obj, void (Type::*method)()) { (obj->*method)(); return true; }

and generic classes to carry the various objects and methods around. Thanks to Mr.Ree for the code that pushed me in that direction!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:28:16+00:00Added an answer on May 18, 2026 at 9:28 pm

    Wouldn’t it be easier to implement an overloaded no-op version of print(void)?

    Ahh well. Function templates and overloading will easily handle this at runtime.

    It gets somewhat stickier if you had wanted to handle this at compile time, for use with #if macros or static-compile-time-asserts.

    But since you only want the former, may I suggest something like this as a starting point:

    (Tested under (GCC) 3.4.4 & 4.0.1. — I know, I need to upgrade!)

    #include <iostream>
    using namespace std;
    
    struct Foo {
      void operator()() {}
    };
    struct Bar {
      bool operator()() { return false; }
    };
    Foo foo;
    Bar bar;
    bool baz() { return false; }
    void bang() {}
    
    
    struct IsVoid
    {
      typedef char YES[1];
      typedef char NO[2];
    
            /* Testing functions for void return value. */
    
      template <typename T>
      static IsVoid::NO  & testFunction( T (*f)() );
    
      static IsVoid::YES & testFunction( void (*f)() );
    
      static IsVoid::NO  & testFunction( ... );
    
            /* Testing Objects for "void operator()()" void return value. */
    
      template <typename C, void (C::*)()>
      struct hasOperatorMethodStruct { };
    
      template <typename C>
      static YES & testMethod( hasOperatorMethodStruct<C, &C::operator()> * );
    
      template <typename C>
      static NO & testMethod( ... );
    
    
            /* Function object method to call to perform test. */
      template <typename T>
      bool operator() (T & t)
      {
        return (    ( sizeof(IsVoid::testFunction(t))  == sizeof(IsVoid::YES) )
                 || ( sizeof(IsVoid::testMethod<T>(0)) == sizeof(IsVoid::YES) ) );
      }
    };
    
    
    #define BOUT(X) cout << # X " = " << boolToString(X) << endl;
    
    const char * boolToString( int theBool )
    {
      switch ( theBool )
      {
        case true:   return "true";
        case false:  return "false";
        default:     return "unknownvalue";
      }
    }
    
    int main()
    {
      IsVoid i;
    
      BOUT( IsVoid()(foo) );
      BOUT( IsVoid()(bar) );
      BOUT( IsVoid()(baz) );
      BOUT( IsVoid()(bang) );
      cout << endl;
    
      BOUT( i(foo) );
      BOUT( i(bar) );
      BOUT( i(baz) );
      BOUT( i(bang) );
    }
    



    Okay, I begin to see more of the problem.

    While we can do something along the lines of this:

    #include <iostream>
    using namespace std;
    
    struct FooA {
      void operator()() {}
    };
    struct FooB {
      bool operator()() { return false; }
    };
    struct FooC {
      int operator()() { return 17; }
    };
    struct FooD {
      double operator()() { return 3.14159; }
    };
    FooA fooA;
    FooB fooB;
    FooC fooC;
    FooD fooD;
    
    void   barA() {}
    bool   barB() { return false; }
    int    barC() { return 17; }
    double barD() { return 3.14159; }
    
    
    namespace N
    {
            /* Functions */
    
      template <typename R>
      R    run( R (*f)() )    { return (*f)(); }
    
      bool run( void (*f)() ) { (*f)();  return true; }
    
    
            /* Methods */
    
      template <typename T, typename R>
      R    run( T & t, R (T::*f)() ) { return (t .* f) (); }
    
      template <typename T>
      bool run( T & t, void (T::*f)() ) { (t .* f) (); return true; }
    };
    
    
    #define SHOW(X) cout << # X " = " << (X) << endl;
    #define BOUT(X) cout << # X " = " << boolToString(X) << endl;
    
    const char * boolToString( int theBool )
    {
      switch ( theBool )
      {
        case true:   return "true";
        case false:  return "false";
        default:     return "unknownvalue";
      }
    }
    
    
    int main()
    {
      SHOW( N::run( barA ) );
      BOUT( N::run( barA ) );
      SHOW( N::run( barB ) );
      BOUT( N::run( barB ) );
      SHOW( N::run( barC ) );
      SHOW( N::run( barD ) );
      cout << endl;
    
      SHOW( N::run(fooA,&FooA::operator()));
      BOUT( N::run(fooA,&FooA::operator()));
      SHOW( N::run(fooB,&FooB::operator()));
      BOUT( N::run(fooB,&FooB::operator()));
      SHOW( N::run(fooC,&FooC::operator()));
      SHOW( N::run(fooD,&FooD::operator()));
    }
    

    You do still have that nasty need to feed &CLASS::operator() in as an argument.


    Ultimately, while we can determine whether the object’s operator() method returns a void, we cannot normally overload based on return types.

    We can get around that overloading limitation via template specialization. But then we get into this uglyness wherein we still need to specify types… Especially the return type! Either manually, or by passing in a suitable argument from which we can extract the necessary types.

    BTW: #define macros won’t help either. Tools like ?: require the same type for both the ? and : part.

    So this is the best I can do…



    Of course…

    If you don’t need the return type…

    If you are just passing the result to another function…

    You can do something like this:

    #include <iostream>
    using namespace std;
    
    struct FooA {
      void operator()() {}
    };
    struct FooB {
      bool operator()() { return false; }
    };
    struct FooC {
      int operator()() { return 17; }
    };
    struct FooD {
      double operator()() { return 3.14159; }
    };
    FooA fooA;
    FooB fooB;
    FooC fooC;
    FooD fooD;
    
    void   barA() {}
    bool   barB() { return false; }
    int    barC() { return 17; }
    double barD() { return 3.14159; }
    
    
    #define SHOW(X) cout << # X " = " << (X) << endl;
    
    namespace N
    {
      template <typename T, typename R>
      R    run( T & t, R (T::*f)() ) { return (t .* f) (); }
    
      template <typename T>
      bool run( T & t, void (T::*f)() ) { (t .* f) (); return true; }
    
    
      template <typename T>
      void R( T & t )
      {
        SHOW( N::run( t, &T::operator() ) );
      }
    
      template <typename T>
      void R( T (*f)() )
      {
        SHOW( (*f)() );
      }
    
      void R( void (*f)() )
      {
        (*f)();
        SHOW( true );
      }
    };
    
    
    int main()
    {
      N::R( barA );
      N::R( barB );
      N::R( barC );
      N::R( barD );
      N::R( fooA );
      N::R( fooB );
      N::R( fooC );
      N::R( fooD );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.