Embedded lambdas suffer from the same latency of indirect function pointer addressing

// approach 1
template <typename T>
void f1(T t, int a, int b) {
   t(a, b);
}

// approach 2
void f2(void(*g)(int, int), int a, int b) 
{
   g(a, b); 
}

void g (int a, int b) 
{
    // do something
}

int main() 
{
   f1([](int a, int b)
         { 
           //do something 
         }, 1, 2);

   f2(&g, 1, 2);
}

My question is: do f1and f2suffer from the same latency of indirect addressing to get the address of the executable function (given that lambda is implemented as a function object)?

What if lambda was not built in?

Note. I declared the function f1 as a template in order to leave the output of the lambda-type parameter to the compiler (instead of, for example, forcing the std function to be used, I’m not sure if it matters, though).

+4
source share
2 answers

. lambda - , , , , , , . , , , . , .

+5

f1 f2 , (, )?

. , - , . , .

, , .

, ?

. .

. f1 , lambda- ( , , , std, , , ).

std::function , .

+3

Source: https://habr.com/ru/post/1665882/


All Articles