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

The Archive Base Latest Questions

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

I was having a look at the Function class documentation in Boost, and stumbled

  • 0

I was having a look at the “Function” class documentation in Boost, and stumbled across this:

boost::function<float (int x, int y)> f;

I must admit this syntax is highly confusing for me. How can this be legal C++ ?

Is there any trick under the hood ? Is this syntax documented anywhere?

  • 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-15T11:38:33+00:00Added an answer on May 15, 2026 at 11:38 am

    [Edit] This is an answer to the author’s original, unedited question which was actually two questions.

    I must admit this syntax is highly
    confusing for me. How can this be
    legal C++ ? 🙂 Is there any trick
    under the hood ? Is this syntax
    documented anywhere ?

    This is perfectly legal and it’s actually not too complicated.

    template <class T>
    class C
    {
    public:
        T* function_pointer;
    };
    
    void fn(int x)
    {
        cout << x << endl;
    }
    
    int main(int argc, char** argv)
    {
        C<void (int x)> c;
        c.function_pointer = &fn;
        c.function_pointer(123); // outputs x
    }
    

    It’s basically the same thing as doing:

    typedef void Function(int);
    C<Function> c;
    

    This type is not just applicable C++, it’s just as applicable in C (the actual type C is parameterized to). The template magic here is taking something like the Function typedef here and being able to detect the types of the return values and arguments. Explaining that would be too lengthy here and boost::function uses a lot of the function traits meta-templates in boost to retrieve that information. If you really want to spend the time to learn this, you should try to understand the boost implementation starting with boost::function_traits in Boost.Type Traits.

    However, I want to address your general problem. I think you are trying too hard to simplify a couple of lines of perfectly acceptable code. There’s nothing wrong with passing the arguments for your command subclasses through their parameterized subclass constructors. Is is really worth trying to involve typelists and boost::function-like solutions here, thereby increasing compile times and code complexity significantly, just for that?

    If you want to reduce it further, just write an execute function that will execute any command subclass and add it to the undo stack and so on:

    typedef boost::shared_ptr<Command> CommandPtr;
    
    void execute(const CommandPtr& cmd)
    {
        cmd->execute();
        // add command to undo stack or whatever else you want to do
    }
    
    // The client can simply write something like this:
    execute(CommandPtr(new CmdAdd(some_value) );
    

    I really think the trade-off of trying to make it any more complicated isn’t worth it. The boost authors wanted to write an extremely general-purpose solution for boost::function which would be used by many people across many platforms and compilers. However, they didn’t try to generalize a command system capable of executing functions with different signatures across a unified undo system (thereby requiring the state of these commands to be preserved even after one is initially finished calling them and be able to undo and re-execute them without re-specifying the original state data on subsequent executions). For that, your inheritance-based approach is most likely the best and most straightforward one.

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

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • 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 Doing this on XP, you can also get the RegMon… May 16, 2026 at 2:04 pm
  • Editorial Team
    Editorial Team added an answer It's quite simple to stream from your computer to Red5… May 16, 2026 at 2:04 pm
  • Editorial Team
    Editorial Team added an answer Try upper case HH for the hour. The standard says… May 16, 2026 at 2:04 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

While developing a design using jQuery I stumbled across a problem. How would I
I am new to jQuery and I am having trouble with the syntax, selectors
Having a look at BDD frameworks and I can't help but wonder which one
I was having a look at the ObservableCollection code (thanks to the awsome .NET
I've had a look round similar articles such as this one and I can't
I am having trouble getting inside my Search WebMethod from my JQuery call. Maybe
How can I write a wrapper that can wrap any function and can be
in C++, I can easily create a function pointer by taking the address of
Im having trouble getting my head round subqueries in Mysql. Fairly simple ones are
I'm having a problem where when I try to select the rows that have

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.