泛型lambda如何在C ++ 14中工作? [英] How does generic lambda work in C++14?

查看:128
本文介绍了泛型lambda如何在C ++ 14中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

泛型lambda如何在C ++ 14标准中工作( auto 关键字作为参数类型)

How does generic lambda work (auto keyword as an argument type) in C++14 standard?

它是基于C ++模板,其中对于每个不同的参数类型编译器生成一个具有相同主体但替换类型(编译期多态性)的新函数,到Java的泛型(类型擦除)?

Is it based on C++ templates where for each different argument type compiler generates a new function with the same body but replaced types (compile-time polymorphism) or is it more similar to Java's generics (type erasure)?

代码示例:

auto glambda = [](auto a) { return a; };


推荐答案

通用lambdas介绍于 C ++ 14

Generic lambdas were introduced in C++14.

简单地说,由lambda表达式定义的闭包类型将具有模板调用运算符,而不是常规的非模板调用操作符 C ++ 11 的lambdas(当然,当 auto 参数列表)。

Simply, the closure type defined by the lambda expression will have a templated call operator rather than the regular, non-template call operator of C++11's lambdas (of course, when auto appears at least once in the parameter list).

所以你的例子:

auto glambda = [] (auto a) { return a; };

会使 glambda

class /* unnamed */
{
public:
    template<typename T>
    T operator () (T a) const { return a; }
};



<给定lambda表达式的闭包类型定义为:

Paragraph 5.1.2/5 of the C++14 Standard Draft n3690 specifies how the call operator of the closure type of a given lambda expression is defined:


非通用lambda表达式的闭包类型具有公用内联函数调用运算符(13.5.4)
,其参数和返回类型分别由lambda-expression的parameter-declaration-clause
和trailing-return-type描述。 对于一个通用的lambda,闭包类型有一个公共内联函数调用
操作符成员模板(14.5.2),其template-parameter-list由一个发明类型template-parameter
在lambda的parameter-declaration-clause中,按照出现的顺序

如果相应的参数声明声明
a函数参数包(8.3.5),则发明的类型template-parameter是一个参数包。函数调用
运算符模板的返回类型和函数参数是从lambda表达式的trailing-return-type和parameter-declarationclause导出的
,通过替换参数的decl-specifier中的每个auto的出现-declaration-clause with
对应的已发明模板参数的名称。

The closure type for a non-generic lambda-expression has a public inline function call operator (13.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively. For a generic lambda, the closure type has a public inline function call operator member template (14.5.2) whose template-parameter-list consists of one invented type template-parameter for each occurrence of auto in the lambda’s parameter-declaration-clause, in order of appearance. The invented type template-parameter is a parameter pack if the corresponding parameter-declaration declares a function parameter pack (8.3.5). The return type and function parameters of the function call operator template are derived from the lambda-expression’s trailing-return-type and parameter-declarationclause by replacing each occurrence of auto in the decl-specifiers of the parameter-declaration-clause with the name of the corresponding invented template-parameter.

最后:


它类似于模板,其中对于每个不同的参数类型编译器生成相同主体但更改类型的函数,或者它更类似于Java的泛型?

Is it similar to templates where for each different argument type compiler generates functions with the same body but changed types or is it more similar to Java's generics?

如上所述,通用lambdas只是用于具有模板调用操作符的唯一未命名函子的语法糖。这应该回答你的问题:)

As the above paragraph explains, generic lambdas are just syntactic sugar for unique, unnamed functors with a templated call operator. That should answer your question :)

这篇关于泛型lambda如何在C ++ 14中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆