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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:13:37+00:00 2026-06-14T02:13:37+00:00

If I do this :- class Thing { … void function (const std::string& message);

  • 0

If I do this :-

class Thing  
{
    ...
    void function (const std::string& message);
};

std::list<std::function<void()>> work;

and in some member of “Thing”

work.push_back(std::bind(&Thing::function, this, "Hello"));

Does either the call to std::bind or the use of std::function<> cause any dynamic memory allocation using new or otherwise? Or is all the storage allocated at compile time? If the standard doesn’t say anything, what about in visual studio 2012 as my program will only need to build on there, and for efficiency I probably need to avoid dynamic memory allocations in the place where I am thinking of using this mechanism.

  • 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-14T02:13:38+00:00Added an answer on June 14, 2026 at 2:13 am

    The standard doesn’t specify, but in general it’s easy to see that std::function must allocate memory at least in some cases:

    struct huge { char c[10000]; };
    void foo(const huge &);
    std::function<void()>{std::bind(foo, huge{})};
    

    On the other hand it’s possible for it to avoid allocation in at least some cases by siting its function object inside a preallocated buffer inside the function object’s footprint; obviously there is a tradeoff as this could make other uses take more stack memory. A good implementation would be able to avoid memory allocation when storing a raw function pointer in a function object, and possibly also for a mem_fn, but it’s less likely that it’d do so for a bind.

    For example, libstdc++ (g++) inlines (functor) object pointers, function pointers, and (non-virtual) member function pointers, as well as anything else that’d fit in the same footprint, e.g. stateless functors (union _Nocopy_types).

    If you can, by inverting your control flow to accept templated functor objects instead of function you can avoid any extra memory allocation:

    template<typename F>
    void my_algorithm(const F &);
    my_algorithm(std::bind(foo, huge{}));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if something like this is possible in Scala: case class Thing(property:String)
I've got a class like this: package org.jjerms.thing; interface IThing { void doSomething(); }
Why is this thing not working? [Database(Name=Relationships_Test)] [Table(Name = Order)] public class Order {
I have this class defintion: @XmlRootElement public class RssRoot { private String _version; private
What I'd like is for this class class Car { public function __construct(Engine $engine,
I've got a model like this: class Thing(models.Model): property1 = models.IntegerField() property2 = models.IntegerField()
This is best described in pseudo-code: class Thing {}; interface ThingGetter<T extends Thing> {
I have this class constructor: public Category(int max){ ... } The thing is, I
Suppose a construct like this: class Interface { public: template <typename T> virtual void
Is this doable? I have the following scope: class Thing < ActiveRecord::Base scope :with_tag,

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.