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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:35:40+00:00 2026-05-22T22:35:40+00:00

Say I have a function that takes a function pointer: int funct(double (*f)(double)); And

  • 0

Say I have a function that takes a function pointer:

int funct(double (*f)(double));

And I pass it a function that doesn’t actually do anything:

double g(double a) { return 1.0;}
//...
funct(g);

Will the compiler optimize out the calls to g? Or will this still have overhead? If it does have overhead, how much? Enough that it is worth overloading the function to receive both function pointers and constant values?

  • 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-22T22:35:40+00:00Added an answer on May 22, 2026 at 10:35 pm

    Newer versions of GCC (4.4 and later) can inline and optimize a known function pointer using the option -findirect-inlining. This only works when GCC also knows all of the code that uses the pointer.

    For example, the C library function qsort will not benefit from this optimization. The compiled machine code for qsort is in the library, it expects a function pointer and the compiler cannot change that.

    However, if you had your own implementation of qsort and you placed it in a header file or you used the very new GCC link-time optimization features, then GCC would be able to take your calling code, the function pointed to, and your qsort source and compile it all together, optimized for your data types and your comparison function.

    Now, the only times that this really matters is when the function call overhead is much larger than the function itself. In your example of a function that does nothing much, using a function pointer is serious overhead. In my example of a qsort comparison, the function pointer call is also quite expensive. But in some other application like Windows event dispatch, it hardly matters.

    Since you are using C++ you should study templates. A template function can accept a so-called function object which is just an object that implements operator() and it can accept function pointers. Passing a function object will allow the C++ compiler to inline and optimize almost all of the code involved.

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

Sidebar

Related Questions

Say I have a function that takes a const reference to a pointer... Example:
Let's say you have a function that returns a date: Date myFunc(paramA, paramB){ //conditionally
Say I have a Python function that returns multiple values in a tuple: def
Say I have a C function which takes a variable number of arguments: How
Let's say I have the following function: sumAll :: [(Int,Int)] -> Int sumAll xs
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer,
Let's say we have a method signature like public static function explodeDn($dn, array &$keys
I passed a pointer ptr to a function whose prototype takes it as const

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.