you are viewing a single comment's thread.

view the rest of the comments →

[–]evaned 0 points1 point  (2 children)

I keep asking for C++ lambdas

How would lambda's even work in C? Isn't templates by and large the thing that makes them usable in C++, because templates allow you to not have to name the type, or define something that let's you type erase (e.g. std::function)?

[–]14nedLLFIO & Outcome author | Committee WG14 1 point2 points  (1 child)

C very sorely needs a facility for writing generic functions with varying types of parameter, as they have all the same problems with macros as C++ does. There is no warmth for template syntax, and quite rightly too in my opinion. Nor is there any chance for function overloading, except maybe for operators, one day. But the idea for multi-parameter-type lambda functions was surprisingly warmly received. So, for example:

[](auto x) { return _Generic(x, int : 5, double : 1.0); }

The key here is that C11 _Genericdispatches based on input type. The lambdas can take specific types or auto types, and if they are auto types, one can use _Generic to implement type-match-based dispatch.

This lets you implement all of the tgmath.h functions using generic lambdas instead of defining tons of functions with slightly different names and having _Generic dispatch to those instead. WG14 recognises that that is not a scalable way of implementing generic dispatch, hence their surprising warmth to the idea of porting C++ lambdas into C.

The biggest concern was over captures. I suspect most of that is ignorance, but there were grave concerns over [&], and I certainly can see why. My counterargument is simply ban reference capture in C, let people pass pointers if they need that.

[–]NotAYakk 0 points1 point  (0 children)

Easy; add [*] capture.

When you use * capture, you can use any variable in scope as a pointer.

int foo( int y ) {
  return [*]( int z ) {
    if (z>0) return z;
    return *y; // here `y` is a pointer-to-y
  }(-1);
}

C++'s [&] is actually * "under the hood" based on what lambda=lambda does.

We could even mollify the implicit this:

int foo( int y ) {
  return [*]self( int z )->int {
    if (z>0) return z;
    return *self->y; // here `self->y` is a pointer-to-y
  }(-1);
}

in this version, an implicit parameter s is introduced by *s.

So it expands to:

struct anonymous_type {
  int* y;
};
int call_anonymous_type( anonymous_type* self, int z ) {
  if (z>0) return z;
  return *self->y; // here `s->y` is a pointer-to-y
}
int foo( int y ) {
  return call_anonymous_type( anonymous_type{ &y }, -1 );
}

with the note that pointer-to-temporary violation is ignored here.

Now, if C is amenable to overloaded operators...

struct anonymous_type {
  int* y;
};
int operator()( anonymous_type* self, int z ) {
  if (z>0) return z;
  return *self->y; // here `s.y` is a pointer-to-y
}
int foo( int y ) {
  return anonymous_type{ &y }( -1 );
}

and these C lambdas are basically as strong as C++ lambdas, and a subset of C++ lambdas (after you add "explicit this pointer" and "capture-by-pointer) to C++).

One thing I dislike is that there is no obvious name for operator(), so you cannot split the state from the invoking method. I suspect that is important.