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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:35:26+00:00 2026-06-12T17:35:26+00:00

My question concerns the application of inline optimisations on function wrappers in C++, consider

  • 0

My question concerns the application of inline optimisations on function wrappers in C++, consider the following code, the WorkerStructure object is initialised with a function wrapper that encapsulates some chunk of functionality. The function wrapper is then used when the WorkerStructure::doSomeWork method is invoked.

Will the functionality encapsulated by the workerFunction object be inlined when applied on the WorkerStructure::doSomeWork method?, obviously if the functionality is defined in some other translation unit, the workerFunction object only encapsulates a function pointer, are there any other circumstances where inlining will not be possible?

When a lambda function defined in a different translation unit is passed via the function wrapper, is it effectively equivalent to passing a function pointer?

struct WorkerStructure
{
    WorkerStructure(std::function <bool(float)> &f):workerFunction(f) {}

    void doSomeWork(float inputValue)
    {
        if(workerFunction(inputValue))
        {
            //do some conditional operation
        }
    }
    std::function <bool(float)> workerFunction ;
};
  • 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-12T17:35:27+00:00Added an answer on June 12, 2026 at 5:35 pm

    The polymorphic nature of std::function inherently makes it very very difficult to actually inline the call. Since a std::function can story any callable entity; how would you write the inlining code?

    It’s somewhat like inlining virtual functions which are called through the base pointer with no other information available (aka no assignment from derived to base pointer prior to the invokation, which the compiler might use to enable inlining).

    Most of the time, std::function is implemented with a void* pointer and a function pointer to a specialization of a templated function, that does the actual invokation and casting and stuff. There are of course variants that use virtual functions to do this, and it’s clearer with them why it’s astonishingly hard. Even link-time opimization won’t be able to do anything, since it doesn’t matter, you already have all the information you can get at the call site (which isn’t much).

    Here’s a very crude version of std::function using the pointer to template function version, dealing only with the store and call aspect (leaving out memory management, copying, moving, resetting, space optimization etc.):

    template<class Sig>
    class function;
    
    template<class R, class... Args>
    class function<R(Args...)>{
      typedef R (*call_type)(void*, Args...);
      void* _obj;
      call_type _caller;
    
    public:
      template<class F>
      function(F f)
        : _obj(new F(f))
        , _caller([](void* p, Args... args){ return (*static_cast<F*>(p))(args...); })
      {}
    
      R operator()(Args... args) const{
        return _caller(_obj, args...);
      }
    };
    

    Live example. I think it’d be very hard to check what is actually inside of _obj and _caller and the point where the function is invoked.

    Just for reference, here’s the version with virtual functions.

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

Sidebar

Related Questions

This question concerns a Tomcat 7 web application, which is connected to a MySQL
My question concerns markup that surrounds some of the default phone number labels in
Question is already been asked in title. Here is a code: (function($){ var filter
My question is concerning an application that will use a host application where the
I'm implementing a small e-shop application in django. My question concerns modelling an Order
I have an application that deals with data in the following structure: struct Message
My question really concerns draging in an outline view, but I'd guess that doesn't
This question concerns XML schemas and files. Suppose I am developing a desktop application
Just a general question, I'm developing an ASP.NET MVC 3 web application that reads
My question concerns Google Web Toolkit (GWT). I'm about to begin development for a

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.