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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:51:03+00:00 2026-05-25T01:51:03+00:00

I am writing a class in which a one of the function’s implementation depends

  • 0

I am writing a class in which a one of the function’s implementation depends on the users. Currently I have it as a virtual function and users need to override my class to provide its implementation. I am thinking of making it a functor(boost::function)/function_pointer so that users can register to it.
Application is extremely performance critical and speed is more important than the classes looking nice. Is changing to functors give some performance benefit?

In most cases, it will be a free function so a function pointer should be fine. But I guess there would be some cases where state may be required and hence it needs to be a functor.

Will I get any preformance benefit by allowing to register either a function_ptr or a functor and calling the appropriate one based on some bool? Something similar to this.

class Myclass
{
   public:
    registerFuncPtr(FuncPtrSignature);
    registerFunctor(boost::function func);
   private:    
   someMethod(someArgs)
   {
    ...
    if(_isFuncPtr)
        _func_ptr(args);
    else
        _functor(args);
    ...
   }
   bool _isFuncPtr;
   FuncptrSignature _func_ptr;
   boost::function _functor;
}

Update:

I am writing a shared library and the clients would dynamically link against it.
No C++0x. 🙁

  • 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-25T01:51:04+00:00Added an answer on May 25, 2026 at 1:51 am

    If you are after performance, go with naked function pointers. If you need state, provide a separate state argument of type void* in your function, and let the users register their state. There’s no magic that can make any C++ construct to be faster than that, since functors and virtual functions and all the rest are all just function pointers wrapped in a more or less type-safe envelope.

    More importantly, when talking about optimization, you should no trust anything whatsoever but your profiler (this includes the paragraph above BTW).

    Inlining could in theory improve the performance, but the users’ functions cannot be inlined (a compiler cannot inline code that hasn’t been written yet). If you don’t compile your library, but distribute it as source code, there’s a possibility of some performance benefit due to inlining. As always, only a profiler can tell.

    One case when a function object is likely to outperform a function pointer is passing them to a function template. If the template itself is not getting inlined, then the function object is likely to get inlined inside the template instantiation, and the function pointer is not.

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

Sidebar

Related Questions

I am writing a template class which takes a floating-point-like type (float, double, decimal,
I am writing unit for a class which looks like following using nunit and
I am writing a .NET wrapper class for an existing native class which throws
I'm writing some code for a class constructor which loops through all the properties
I'm writing a simple C++ class in which I would like cache picture thumbnails
I am writing a Xml Tag Renamer class with Java which reads in a
I'm writing a sparse matrix class in C++ in which every row and column
I am writing a custom flex preloader which extends the IPreloaderDisplay class. How can
I've got a method in a class that's writing to some string, which calls
Consider this... I am writing a class named Cache which will accept either 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.