简体   繁体   English

C++20模板lambas的限制和使用

[英]limits and uses of C++20 template lambas

A couple of related questions for C++ standard gurus. C++ 标准专家的几个相关问题。

The incoming C++20 introduces template lambdas ( P0428R2 ).传入的 C++20 引入了模板 lambdas ( P0428R2 )。

So instead of所以代替

auto x = [](auto x, auto y){ return x+y; };

we can specify the template parameter as follows我们可以指定模板参数如下

auto x = []<typename T>(T x, T y){ return x+y; };

So far, so good.到现在为止还挺好。

First question: can explicit template parameters, in template lambdas, only be deduced from arguments, or is it possible to add non-deduced template arguments?第一个问题:模板 lambda 中的显式模板参数是否只能从参数中推导出来,还是可以添加非推导出的模板参数?

Reading P0428r1 I don't see any explicit limitations but, also, I don't see examples of non-deduced template arguments.阅读 P0428r1 我没有看到任何明确的限制,但是,我也没有看到非推导模板参数的例子。

In first approximation I suppose that non-deduced template arguments are legal because I see that the following silly code在第一个近似值中,我认为非推导的模板参数是合法的,因为我看到以下愚蠢的代码

int main()
 {   
   []<int = 0>(){ }();
 }

compiles and runs with both g++ (10.0.0 head) and clang++ (10.0.0 head).使用 g++(10.0.0 head)和 clang++(10.0.0 head)编译和运行。

Supposing that non-deduced template parameters are allowed, the second question is: how can I call a template lambda while providing a template parameter?假设允许非推导的模板参数,第二个问题是:如何在提供模板参数的同时调用模板 lambda?

By example: given the following template lambda通过示例:给定以下模板 lambda

auto x = []<std::size_t I>(auto t){ return std::get<I>(t); };

Is there some syntax for specifying the template parameter I when invoking such lambdas without explicitly naming operator() ?在调用此类 lambda 时是否有一些语法用于指定模板参数I而不显式命名operator()

I've tried with我试过

x<0u>(y);

but the < is interpreted as a relational operator.<被解释为关系运算符。

I've tried simply adding template我试过简单地添加template

x template <0u>(y);

but it doesn't work.但它不起作用。

There are no special restrictions on template headers in lambda functions. lambda 函数中的模板头没有特殊限制。 Lambdas are after all just shorthand for what you could do already with any operator() overload. Lambdas 毕竟只是你可以用任何operator()重载做的事情的简写。

There is no special syntax for providing template arguments when invoking the operator() of a lambda function.在调用 lambda 函数的operator()时,没有用于提供模板参数的特殊语法。 If you have template parameters which are not deduced, you will have to use the traditional mechanisms for providing those template arguments.如果您有未推导的模板参数,则必须使用传统机制来提供这些模板参数。 IE: lamb.operator()<Args>(...) .即: lamb.operator()<Args>(...)

Non-deduced lambda template arguments are legal.非推导的 lambda 模板参数是合法的。 The syntax for calling them is similar to the existing function notation required when a method calls an overloaded operator of the same class;调用它们的语法类似于方法调用同一类的重载运算符时所需的现有函数符号; and especially if it is an overloaded operator template.特别是如果它是一个重载的运算符模板。

I show the most verbose combination in the example below, where the template keyword is also required as the lambda has a dependent name:我在下面的示例中展示了最详细的组合,其中template关键字也是必需的,因为 lambda 有一个依赖名称:

#include <tuple>

template <typename T>
void test()
{
  std::tuple tup{42, "eggs"};
  auto x = []<std::size_t I>(auto t){ return std::get<I>(t); };
  int i = x.template operator()<0>(tup);
}

int main(int argc, char *argv[])
{
  test<float>();
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM