1

Imagine following toy example of templated lambda:

auto f = []<typename T>(){};

The shortest way I know to call this lambda for int type is f.operator()<int>();.

Try it online

Does anybody know if there is simpler/shorter syntax for calling templated lambda?

Of cause f<int>(); doesn't compile, but would be nice to have such syntax similar to template function call.

But maybe in C++ standard they invented for lambda some special shortcut alias that I don't know about, like f._<int>();, here imaginary _ is a name of method that just perfect-forwards all args to .operator() method. Is there anything like that? Or maybe f.call<int>()?

I understand that lambda behaves like simple functor class with method .operator(), hence same long call syntax as for templated functor f.operator()<int>(). But in your custom functor you can invent alias method _ that forwards args, is there such similar alias in lambda?

As @RedFog told it is possible to implement a helper function that you use like call<int>(f); but I don't know how to implement such function for very general case - imagine if lambda has a mix of typename and auto params, like []<typename A, auto B, typename Q>{};, I don't know how to write very generic helper function for such mixed case.

Arty
  • 14,883
  • 6
  • 36
  • 69
  • what about `call(f)` or `call(f)()`? – RedFog Apr 24 '21 at 08:32
  • @RedFog You mean to create external helper function? As a backup solution could be, thanks! But I'm curious if standard lambda may have some ready-made `_()` method for doing such shortcuts. Need some experts to tell us for sure if such extra method exists or not. – Arty Apr 24 '21 at 08:35
  • @RedFog Also helper function can't probably cover all template params types. Imagine if lambda has long list of combinations of `auto` and `typename` params like `[]{};` then I don't know how to implement a helper `call()` function for supporting any list of template params. – Arty Apr 24 '21 at 08:37
  • yes, it can not solve all the problem, just for the usual case of `typename...`. but to *if standard lambda may have some ready-made `_()` method for doing such shortcuts*, I'm afraid not. – RedFog Apr 24 '21 at 08:56
  • 1
    Given that there isn’t any special syntax, [limits and uses of C++20 template lambas](https://stackoverflow.com/questions/59592122/limits-and-uses-of-c20-template-lambas) seems like a duplicate. – Davis Herring Apr 24 '21 at 15:09

0 Answers0