7

What is the reason why lambdas have no default constructor? Is there any technical reason behind that, or is it a pure design decision?

Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 1
    What would be the purpose of it? It is constructed when you declare it. – NathanOliver Dec 16 '16 at 22:32
  • 4
    What do you envisage a default constructor doing? –  Dec 16 '16 at 22:32
  • @NathanOliver, I would have liked a default constructed lambda function in [this answer](http://stackoverflow.com/a/41189224/434551). Think of it as the analogue of `nullptr` for pointers. – R Sahu Dec 16 '16 at 22:45
  • A constructor implies the existence of a class. A lambda expression may generate a class or not. But even if it does, what name could it have? For a lambda that carries no state, it would not even make sense to be implemented as a class, but rather a function. – John Griffin Dec 16 '16 at 22:48
  • 3
    @JohnGriffin Each lambda expression has a corresponding closure type. I don't think there's a technical reason for that decision. – Columbo Dec 16 '16 at 23:01
  • @Columbo Could it be due to the fact that an element captured by reference can result in an unnamed non-static data member? The default constructor wouldn't work in this case: `int main() { int i; auto l1 = [&i](){}; decltype(l1) l2; }`. – skypjack Dec 16 '16 at 23:06
  • @skypjack That's not a reason for not providing a default constructor; it would simply be defined as deleted. – Columbo Dec 16 '16 at 23:07
  • @Columbo Touché. It makes sense actually. Thank you. – skypjack Dec 16 '16 at 23:13
  • 1
    What does it mean "no" vs "deleted" in _"Closure types have a deleted (until C++14)no (since C++14) default constructor."_ from [cppreference](http://en.cppreference.com/w/cpp/language/lambda)? – Spectral Sequence Dec 16 '16 at 23:20
  • This might sound trite but lambdas are functions, in the spirit of a lambda calculus. Functions do not have run-time constructors. (yes, function containers like std::function() do but not functions). – JimmyNJ Dec 17 '16 at 02:53
  • @JimmyNJ : Lambdas are _functors_, otherwise they couldn't have captures. – ildjarn Dec 17 '16 at 06:57
  • @ildjarn , if lambda functions are functors why don't they have default c'tors? That a lambda captures local variables isn't terribly relevant, any function call can capture local variables, you just have to type the arguments explicitly. Implementation details aside, the core question remains why don't functions have constructors and I think my answer stands in the spirit of lambda calculus. – JimmyNJ Dec 17 '16 at 17:13
  • 1
    @JimmyNJ : The bottom line is that a lambda creates an _object_ of a _type_ with `operator()`, not a function. – ildjarn Dec 17 '16 at 17:16
  • @ildjarn : I agree from the point of view of C++ OO. But I'd like to know if you can think of an example of a pure function (say in the plain old "C" sense) which is truly run time defined? – JimmyNJ Dec 17 '16 at 17:21
  • 1
    @SpectralSequence: "no" means that lambdas may have constructors, just not a known type, and by virtue of having those constructors, the default one is not generated. (implicitly deleted)... I think. Lambdas I believe will be subject to aggregate initialization in C++17, so we can compose function objects via inheritance from multiple lambdas. See comments on [this answer](http://stackoverflow.com/q/40983367/27678) – AndyG Dec 20 '16 at 18:41

1 Answers1

2

Lambdas in C++ are a syntactic convenience to allow the programmer to avoid declaring a functor using traditional syntax like

struct my_functor
{
bool operator()(Object &) { /* do something */ return false; }
}

because writing functors using this syntax is considered too long winded for simple functions.

Lambdas offer parameter capture options e.g [=], [&] etc. , to save you declaring variables and initializing them, like you might do in the constructor of a functor object.

So Lambdas don't expose any constructor syntax to you by design.

There is no technical reason a lambda function could not expose a constructor, however if it were to do so it would stop mimicking the design concept it is named for.


With thanks to the commentators under the question.

JimmyNJ
  • 1,134
  • 1
  • 8
  • 23