0

This question origins from another question that Why isn't a lambda that captures variables by reference convertible to a function pointer? . It seems like that the lambda with a capture list is implemented as a local class with operator ().

As far as I know, lambda without capture list could be converted to a function pointer, which seems like it's just a ordinary function. What's more, I could not find anything special when compared the lambda without capture list with ordinary function.

At the same time, I think there is no necessity to not implement lambda without capture list as ordinary function.

choxsword
  • 3,187
  • 18
  • 44
  • " I think there is no necessity to not implement lambda without capture list as ordinary function." I use lambda very often and it feels comfortable to *can* do this. Writing a stand alone function and take the address is simply more work todo. And it splits function definition and usage which *can* be avoided by using capture less lambda. So simply I have the necessity to use it ;) – Klaus May 22 '18 at 08:51
  • 2
    Please do `not` use `random` code `markups`. – T.C. May 22 '18 at 08:53
  • @T.C. OK, I've re-edited the post. – choxsword May 22 '18 at 08:58
  • 2
    Do mean by the compiler or as a programmer? – king_nak May 22 '18 at 09:01
  • The capture-less lambda could still be a class object, but with a conversion operator returning a function pointer. It doesn't *have* to be a function itself. – Bo Persson May 22 '18 at 09:09
  • @BoPersson Where does that function pointer comes from? Is it converted form member function? – choxsword May 22 '18 at 09:10
  • @BoPersson Lambdas _mustn't_ be a function. It must have unique type – Passer By May 22 '18 at 09:13
  • Check the interwebs: [Demystifying C++ lambdas](https://blog.feabhas.com/2014/03/demystifying-c-lambdas/). – JHBonarius May 22 '18 at 09:17
  • 1
    `I think there is no necessity to not implement lambda without` triple negative! A new record! – UmNyobe May 22 '18 at 09:17
  • @JHBonarius Wow, that's good. – choxsword May 22 '18 at 09:19
  • 2
    @PasserBy - It must have a unique type only if someone is checking. If all we do is convert lambdas to function pointers, how do we know if they have different types? Anyway, my point was supposed to be that you can be *convertible* to a function pointer without *being* a function. – Bo Persson May 22 '18 at 09:21
  • Am I right, that you are interested in an answer specifically for C++ and perhaps the implementation details of a specific compiler, or do you want a more general answer? – Michael Beer May 22 '18 at 09:22
  • @MichaelBeer This question is asking about implementation details. – choxsword May 22 '18 at 09:25

2 Answers2

1

What's more, I could not find anything special when compared the lambda without capture list with ordinary function.

Try to take the sizeof of a function and then from a non-capturing lambda. You will notice that the function doesn't have a size (and so it fails to compile), while the lambda does (it's almost always 1)!

So the compiler cannot generate a plain function for the non-capturing lambda. Note that it can and implement an exception for lambda functions under the as-if rule, but I wouldn't call this an ordinary function.

By making the call operator static or working around it (compilers can do a lot of stuff that we cant), the conversion to a function pointer can work.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
1

Advantage of structure functor over regular function is that you keep type, which might help in inlining:

std::sort(std::begin(v), std::end(v), &function);

would have to remember the value of &function as bool (*)(const T&, const T&) to allow inlining.

Whereas, for:

std::sort(std::begin(v), std::end(v),
          [](const T& lhs, const T& rhs){ return function(lhs, rhs);});

we have the value anytime we have the comparer. No additional work to remember initial value

Jarod42
  • 203,559
  • 14
  • 181
  • 302