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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:05:32+00:00 2026-05-15T20:05:32+00:00

I have structure for storing callback function like this: template<class T> struct CommandGlobal :

  • 0

I have structure for storing callback function like this:

template<class T>
struct CommandGlobal : CommandBase
{
    typedef boost::function<T ()> Command;
    Command comm;

    virtual T Execute() const
    {
        if(comm)
            return comm();
        return NULL;
    }
};

Seems like it should work fine except when T is void because the Execute function wants to return a value..

What is the best solution to this problem?

Thanks!

  • 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-15T20:05:33+00:00Added an answer on May 15, 2026 at 8:05 pm

    This answer is based off this fun-fact: In a function returning void, you can return any expression of which the type is void.

    So the simple solution is:

    virtual T Execute() const
    {
        if (comm) 
            return comm();
        else
            return static_cast<T>(NULL);
    }
    

    When T = void, the last return statement is equivalent to return;.


    However, I feel this is bad design. Is NULL meaningful for every T? I don’t think so. I would throw an exception:

    virtual T Execute() const
    {
        if (comm)
            return comm();
        else
            throw std::runtime_error("No function!")
    }
    

    However, this is done automatically by Boost, so your code becomes the much cleaner:

    virtual T Execute() const
    {
        return comm();
    }
    

    You could then add additional functionality, such as:

    bool empty(void) const
    {
        return !comm; // or return comm.empty() if you're the explicit type
    }
    

    So the user can check if it can be called prior to calling it. Of course at this point, unless your class has additional functionality you’ve left out for the sake of the question, I see no reason not to just use boost::function in the first place.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't say how you're using Jackson in Spring, so… May 16, 2026 at 2:40 pm
  • Editorial Team
    Editorial Team added an answer According to man fork child's signal handlers are inherited from… May 16, 2026 at 2:40 pm
  • Editorial Team
    Editorial Team added an answer SampleCB as declared in the interface doesn't have the third… May 16, 2026 at 2:40 pm

Trending Tags

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

Top Members

Related Questions

I have a document structure in Solr that looks something like this (irrelevant fields
I have the following structure. public class ToolSettings { public string Extension { get;
What is a good data structure for storing phone numbers in database fields? I'm
Is there a published data structure for storing periodic or recurring dates? Something that
I have a special list (a sort of queue, as in the data structure,
Hi, I have 20 strings, each of which will have same package structure except
This is a bit of an involved problem, so I'll do the best I
The C function myfunc operates on a larger chunk of data. The results are
Just out of curiosity, for applications that have a fairly complicated module tree, would
I have two identical servers. Both Win2k3. I have a web service that queues

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.