mardi 8 juin 2021

What is the possible purpose of this lambda usage? [duplicate]

I found a piece of code from a co-worker who no longer works for the same company as me, so I can't ask them myself.

This was the original code (1):

Class Foo {
  void bar(InType i, OutType* o);
};

void Foo::bar(InType i, OutType* o) {
  o = something();
  if (!o->valid()) throw Exception();
}

They changed it to (2):

Class Foo {
  OutType bar(InType i);
};

auto Foo::bar(InType i) -> OutType {
  OutType o;
  o = something();
  if (!o->valid()) throw Exception();
  return o;
}

This appears the same as (3):

Class Foo {
  OutType bar(InType i);
};

OutType Foo::bar(InType i) {
  OutType o;
  o = something();
  if (!o->valid()) throw Exception();
  return o;
}

I understand that #2 is a member variable that is a lambda, but what do this gain? The lambda is still called just like a regular function syntactically.

Is there a real reason to use #2 instead of #3?

Aucun commentaire:

Enregistrer un commentaire