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 540825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:16:49+00:00 2026-05-13T10:16:49+00:00

If the function pointer embedded in a boost::bind return object is NULL / nullptr

  • 0

If the function pointer embedded in a boost::bind return object is NULL/nullptr/0, I need to take action other than calling it. How can I determine if the object contains a null function pointer?

Addenda

  1. I don’t believe I can use and compare boost::functions as the boost::bind return object is used with varying call signatures in a template function.
  2. Simplified example:
template <typename BRO>
Retval do_stuff(BRO func, enum Fallback fallback)
{
    if (func == NULL)
    {
        return do_fallback(fallback);
    }
    else
    {
        return use_retval(func());
    }
}

do_stuff(boost::bind(FuncPtrThatMightBeNull, var1, var2), fallback);

Solution

Since the arity of the function in the callee does not change, I can “cast” the bind return object into a boost::function and call .empty()

Retval do_stuff(boost::function<Retval()> func, enum Fallback fallback)
{
    if (func.empty())
        return do_fallback(fallback);
    else
        return use_retval(func());
}
  • 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-13T10:16:49+00:00Added an answer on May 13, 2026 at 10:16 am

    You can either bind to a dummy function:

    void dummy() { /* has differing behaviour */ }
    // ...
    boost::bind(&dummy)();
    

    … or, assuming you’re using Boost.Bind together with Boost.Function, return a default constructed function object and check for empty() before calling it:

    typedef boost::function<void (void)> F;
    F create() { return F(); }
    
    void use() {
        F f = create();
        if(f.empty()) {
            /* ... */
        }
    }
    

    Regarding the update:
    I still don’t see what the problem with binding to a different function like the following would be:

    template <typename BRO>
    Retval do_stuff(BRO func)
    {
        return func();
    }
    
    if(funcPtr) {
        do_stuff(boost::bind(&use_retval, boost::bind(funcPtr, a, b)));
    } else {
        do_stuff(boost::bind(&do_fallback, fallback));
    }
    

    If you’d want to move that handling out of the calling code, you could emulate variadic template function to support variable arities:

    template<class R, class T1> 
    boost::function<R (T1)> 
    bind_wrap(R (*fnPtr)(), T1& t1, Fallback fallback) {
        if(fnPtr) return boost::bind(&use_retval,  boost::bind(funcPtr, t1));
        else      return boost::bind(&do_fallback, fallback);
    }
    
    template<class R, class T1, class T2> 
    boost::function<R (T1, T2)> 
    bind_wrap(R (*fnPtr)(T1, T2), T1& t1, T2& t2, Fallback fallback) {
        if(fnPtr) return boost::bind(&use_retval,  boost::bind(funcPtr, t1, t2));
        else      return boost::bind(&do_fallback, fallback);
    }
    
    // ... etc. for all needed arities
    
    do_stuff(bind_wrap(funcPtr, var1, var2, fallback));
    

    … or you use the approach above to generate boost::function<> objects or your own wrappers and check for functor.empty() or similar in do_stuff().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 290k
  • Answers 290k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apparently, the IIS7 did not accept using HttpContext.Error, like this:… May 13, 2026 at 5:53 pm
  • Editorial Team
    Editorial Team added an answer Not in any CRT implementation that I've ever seen. It… May 13, 2026 at 5:53 pm
  • Editorial Team
    Editorial Team added an answer No. The methods may be too simple to break right… May 13, 2026 at 5:53 pm

Related Questions

I am writing a large C program for embedded use. Every module in this
I have a program in which I want to be able to store certain
I'm at a loss to explain what seems to be mis-addressing within the char[]
I have used AJAX to successfully change the content of a web page. I
I am working on an embedded application where the device is controlled through a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.