Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Familiar template syntax for generic lambdas

For c++20 it is proposed to add the following syntax for generic lambdas p0428r2.pdf

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

But the current implementation in gcc 8 did not accept the following instantiation:

f<std::string>("");

Is that a implementation bug in gcc or a missing language feature? I know we talk about a proposal and not a approved specification.

Complete example ( with comparison to template function syntax ):

template <typename T> void n( T t ) { std::cout << t << std::endl; }

auto f = []<typename T>( T t ) { std::cout << t << std::endl; };

int main()
{
    f<std::string>("Hello");  // error!
    n<std::string>("World");
}

complains with following error:

main.cpp:25:22: error: expected primary-expression before '>' token f("Hello");

like image 328
Klaus Avatar asked Jun 06 '18 06:06

Klaus


People also ask

Can you template a lambda?

Lambda-expressions are not allowed in unevaluated expressions, template arguments, alias declarations, typedef declarations, and anywhere in a function (or function template) declaration except the function body and the function's default arguments.

What is generic lambda?

Conceptually, a generic lambda is equivalent to a function object with a templatized function-call operator method: struct LambdaClosureType { ... template<template-params> ret operator()(params) const { ... } .... };


1 Answers

The result of a lambda expression is not a function; it is a function object. That is, it is a class type that has an operator() overload on it. So this:

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

Is equivalent to this:

struct unnamed
{
  template<typename T>
  void operator()(T t) {}
};

auto f = unnamed{};

If you want to explicitly provide template arguments to a lambda function, you have to call operator() explicitly: f.operator()<template arguments>(parameters);.

like image 183
Nicol Bolas Avatar answered Sep 23 '22 10:09

Nicol Bolas