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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:21:36+00:00 2026-06-13T19:21:36+00:00

What is the difference between these two function prototypes ? void apply1(double(f)(double)); void apply2(double(*f)(double));

  • 0

What is the difference between these two function prototypes ?

void apply1(double(f)(double));
void apply2(double(*f)(double));

If the goal is to apply the provided function to an array, is there a version faster compared to the other one ?

EDIT :
An example of implementation :

#include <iostream>
#include <vector>
#include <cmath>

// First version
template<typename Type> void apply1(std::vector<Type>& v, Type(f)(Type))
{
    for (unsigned int i = 0; i < v.size(); ++i) {
        v[i] = f(v[i]);
    }
}

// Second version
template<typename Type> void apply2(std::vector<Type>& v, Type(*f)(Type))
{
    for (unsigned int i = 0; i < v.size(); ++i) {
        v[i] = f(v[i]);
    }
}

// Main
int main()
{
   std::vector<double> v = {1., 2., 3., 4., 5.};
   apply1(v, std::sin);
   apply2(v, std::sin);
   return 0;
}
  • 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-13T19:21:38+00:00Added an answer on June 13, 2026 at 7:21 pm

    First, the speed of the template wrapper instantiation is going to be almost entirely at the mercy of your optimizer.

    That said, I’ve reduced your samples to the most basic code I can think of, specifically to check the invoke of the function parameters. You can read on, but you’ll see they invoke exactly the same. There is no benefit for one declaration vs another. Further, I included the one you left out, (reference-decl)

    #include <cstdio>
    
    int hello(int x)
    {
        return x;
    }
    
    template<typename Type> 
    void apply1(Type x, Type (f)(Type))
    {
        f(x);
    }
    
    template<typename Type> 
    void apply2(Type x, Type (*f)(Type))
    {
        f(x);
    }
    
    template<typename Type> 
    void apply3(Type x, Type (&f)(Type))
    {
        f(x);
    }
    
    int main(int argc, char *argv[])
    {
        apply1(1,hello);
        apply2(2,hello);
        apply3(3,hello);
        return 0;
    }
    

    The actual asm generated from the deductions is:

    apply1

    __Z6apply1IiEvT_PFS0_S0_E:
    Leh_func_begin2:
        pushq   %rbp
    Ltmp2:
        movq    %rsp, %rbp
    Ltmp3:
        subq    $16, %rsp
    Ltmp4:
        movl    %edi, -4(%rbp)
        movq    %rsi, -16(%rbp)
        movq    -16(%rbp), %rax
        movl    -4(%rbp), %ecx
        movl    %ecx, %edi
        callq   *%rax
        addq    $16, %rsp
        popq    %rbp
        ret
    Leh_func_end2:
    

    apply2

    __Z6apply2IiEvT_PFS0_S0_E:
    Leh_func_begin3:
        pushq   %rbp
    Ltmp5:
        movq    %rsp, %rbp
    Ltmp6:
        subq    $16, %rsp
    Ltmp7:
        movl    %edi, -4(%rbp)
        movq    %rsi, -16(%rbp)
        movq    -16(%rbp), %rax
        movl    -4(%rbp), %ecx
        movl    %ecx, %edi
        callq   *%rax
        addq    $16, %rsp
        popq    %rbp
        ret
    Leh_func_end3:
    

    apply3

    __Z6apply3IiEvT_RFS0_S0_E:
    Leh_func_begin4:
        pushq   %rbp
    Ltmp8:
        movq    %rsp, %rbp
    Ltmp9:
        subq    $16, %rsp
    Ltmp10:
        movl    %edi, -4(%rbp)
        movq    %rsi, -16(%rbp)
        movq    -16(%rbp), %rax
        movl    -4(%rbp), %ecx
        movl    %ecx, %edi
        callq   *%rax
        addq    $16, %rsp
        popq    %rbp
        ret
    Leh_func_end4:
    

    They are identical (as I suspected they would be). There is no difference that I can see whatsoever.

    Note: it is worth mentioning the way the compiler saw these declarations by name mangling examination:

    apply1: __Z6apply1IiEvT_PFS0_S0_E
    apply2: __Z6apply2IiEvT_PFS0_S0_E
    apply3: __Z6apply3IiEvT_RFS0_S0_E
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between these two function pointer notations in C? void (*a[]())
In Javascript is there any difference between these two ways of adding a function
Is there any difference between these two: var test1 = function () { this.method1
What is the difference between these two functions? 1: $(document).ready(function myfunc() { function dosomething()
Will there be a difference between the following two ways of calling a function
1)Is there any difference between these two keywords for the elements of collections??( Copy
Is there any difference between these two LINQ statements: var query = from a
What is the difference between these two? collapse: function(fold) { ... ... } and
Is there a difference between the two codes below, I presume not. function Agent(bIsSecret)
What is the difference between these two function calls in PHP? init_get($somevariable); @init_get($somevariable);

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.