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

查看:25
本文介绍了泛型 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; };

推荐答案

泛型 lambda 是在 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 成为这种类型的实例:

Will make glambda an instance of this type:

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

C++14 标准草案 n3690 的第 5.1.2/5 段指定了如何定义给定 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 表达式的参数声明子句描述和尾随返回类型分别.对于泛型 lambda,闭包类型具有公共内联函数调用运算符成员模板(14.5.2),其模板参数列表由一个发明的类型模板参数组成对于 lambda 的参数声明子句中每次出现的 auto,按出现顺序.如果相应的参数声明声明,则发明的类型模板参数是一个参数包一个函数参数包(8.3.5).函数调用的返回类型和函数参数运算符模板源自 lambda 表达式的尾随返回类型和参数声明子句通过将参数声明子句的声明说明符中每次出现的 auto 替换为相应的发明模板参数的名称.

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?

正如上面的段落所解释的,泛型 lambda 只是具有模板化调用运算符的独特、未命名函子的语法糖.那应该回答你的问题:)

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天全站免登陆