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

  • SEARCH
  • Home
  • 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 8119021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:40:39+00:00 2026-06-06T04:40:39+00:00

There have been proposals for C++ delegates which have lower overhead than boost::function :

  • 0

There have been proposals for C++ “delegates” which have lower overhead than boost::function:

  • Member Function Pointers and the Fastest Possible C++ Delegates
  • Fast C++ Delegate
  • The Impossibly Fast C++ Delegates

Have any of those ideas been used to implement std::function, resulting in better performance than boost::function? Has anyone compared the performance of std::function vs boost::function?

I want to know this specifically for the GCC compiler and libstdc++ on Intel 64-bit architectures, but information on other compilers is welcome (such as Clang).

  • 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-06-06T04:40:41+00:00Added an answer on June 6, 2026 at 4:40 am

    In libstdc++’s std::function we use a union type that is suitably sized and aligned to store pointers, function pointers or pointers to member functions. We avoid a heap allocation for any function object that can be stored in that size and alignment, but only if it is “location invariant”

    /**
     *  Trait identifying "location-invariant" types, meaning that the
     *  address of the object (or any of its members) will not escape.
     *  Also implies a trivial copy constructor and assignment operator.
     */
    

    The code is based on the std::tr1::function implementation and that part hasn’t changed significantly. I think that could be simplified using std::aligned_storage and could be improved by specializing the trait so that more types are identified as location invariant.

    Invoking the target object is done without any virtual function calls, the type erasure is done by storing a single function pointer in the std::function which is the address of a function template specialization. All operations are done by calling that function template through the stored pointer and passing in an enum identifying what operation it is being asked to perform. This means no vtable and only a single function pointer needs to be stored in the object.

    This design was contributed by the original boost::function author and I believe it is close to the boost implementation. See the Performance docs for Boost.Function for some rationale. That means it’s pretty unlikely that GCC’s std::function is any faster than boost::function, because it’s a similar design by the same person.

    N.B. our std::function doesn’t support construction with an allocator yet, any allocations it needs to do will be done using new.


    In response to Emile’s comment expressing a desire to avoid a heap allocation for a std::function which holds a pointer to member function and an object, here’s a little hack to do it (but you didn’t hear it from me 😉

    struct A {
      int i = 0;
      int foo() const { return 0; }
    };
    
    struct InvokeA
    {
      int operator()() const { return a->foo(); }
      A* a;
    };
    
    namespace std
    {
      template<> struct __is_location_invariant<InvokeA>
      { static const bool value = true; };
    }
    
    int main()
    {
      A a;
      InvokeA inv{ &a };
    
      std::function<int()> f2(inv);
    
      return f2();
    }
    

    The trick is that InvokeA is small enough to fit in the function‘s small object buffer, and the trait specialization says it’s safe to store in there, so the function holds a copy of that object directly, not on the heap. This requires a to persist as long as the pointer to it persists, but that would be the case anyway if the function‘s target was bind(&A::foo, &a).

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

Sidebar

Related Questions

There have been many proposals to enhance the security of JSONP communications - some
There have been a few occasions in which I've got some code in some
There have been several questions posted to SO about floating-point representation. For example, the
There have been many debates about this topic already here, but none of them
There have been several questions over the past few days about the proper use
There have been a number of problems related to IE8 breaking Visual Studio 2005/2008.
There have been questions with answers on how to write rubygems, but what should
There have been some similar questions asked regarding Grid views, but none have been
There have been multiple discussions on this topic in the site but I am
If there have been commits and many changes since an earlier commit, is there

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.