dimanche 26 septembre 2021

using named lambda vs function - extra space usage for a variable in case of lambda?

So simplified code snippet (1) would be

void f2(someClass* ptr, int x)
{
//some code
}

void f(someClass* ptr, bool cond)
{
//do something with ptr
    if (someConditionThatIsResultOfFunctionWork)
   {
    if (cond)
    {
      //do something else with ptr
      f2(ptr, x1);
    }
     else
        {
          //do something else with ptr
          f2(ptr, x2);
        } 
    }
}

code snippet (2):

void f(someClass* ptr, bool cond)
{
 auto f2asLambda = [](someClass* ptr, int x)
{
//some code
};
//do something with ptr
if (someConditionThatIsResultOfFunctionWork)
{
if (cond)
{
  //do something else with ptr
  f2asLambda(ptr, x1);
}
 else
    {
      //do something else with ptr
      f2asLambda(ptr, x2);
    } 
}
}

Are those code snippets same in terms of performance etc? In case (2) extra variable will be created for the lambda, and we might not call it at all, does it make using function over named lambda preferable?

Aucun commentaire:

Enregistrer un commentaire