简体   繁体   English

为什么C ++ 20模板lambda使用typename关键字?

[英]Why are C++20 template lambdas using typename keyword?

I understand the consistency argument, but most of the parameters for templates are types so I feel that since lambdas are meant to be concise way of defining a struct it probably should have been defaulted to typename / class (you would still need to write int/size_t/short ). 我理解一致性参数,但是模板的大多数参数都是类型,因此我认为,由于lambda是用于定义结构的简洁方法,因此它可能应该默认为typename / class (您仍然需要编写int/size_t/short )。

If somebody is not familiar with changes to lambdas in C++20 here is the example: 如果有人不熟悉C ++ 20中对lambda的更改,请参考以下示例:

[]<typename T>(const std::vector<T>& v)
{
    for(const auto& x : v) { std::cout << x; }
};

and my question is why not: 我的问题是为什么不这样做:

[]<T>(const std::vector<T>& v)
{
    for(const auto& x : v) { std::cout << x; }
};

The problem is that this already has a meaning: 问题在于,这已经具有含义:

template <T> void foo();

It's a function template with one template parameter that is a non-type template parameter whose type is T , and that template parameter has no name. 这是具有一个模板参数的功能模板,该模板参数是类型为T非类型模板参数,并且该模板参数没有名称。

It would be pretty confusing if the same syntax meant very different things depending on if you're introducing a function template or a generic lambda -- which is to say, two very similar contexts serving similar purposes! 如果相同的语法意味着非常不同的事情,这取决于您要引入函数模板还是泛型lambda,即两个具有相似用途的非常相似的上下文,那将非常令人困惑。

Plus then... what would you do if you actually want a non-type template parameter? 再加上...如果您实际上想要一个非类型的模板参数,您将怎么办? Just can't have one? 只是不能拥有一个?

While conciseness is indeed a selling point of lambdas, it is not important enough to supersede the need for consistency. 虽然简洁确实是lambda的卖点,但它不足以取代对一致性的需求。 This is especially true for features involving templates, as they are harder to grasp than non-template language features. 对于涉及模板的功能尤其如此,因为与非模板语言功能相比,它们更难掌握。

In particular, the template syntax for generic lambdas has a narrow scope in the first place, ie, most lambdas can live without it (in fact, the example you gave is a perfect example of not to use it as the function body doesn't instantiate a T or does something comparable). 特别是,通用lambda的模板语法首先具有狭窄的范围,即,大多数lambda可以在没有它的情况下生存(实际上,您给出的示例是不使用它的完美示例,因为函数体没有实例化一个T或做类似的事情。 From P0428 (emphasis mine): P0428 (强调我的):

There are a few key reasons why the current syntax for defining generic lambdas is deemed insufficientby the author. 作者认为当前定义通用lambda的语法不足的几个关键原因。 The gist of it is that some things that can be done easily with normal functiontemplates require significant hoop jumping to be done with generic lambdas, or can't be done at all. 其要点是,使用常规函数模板可以轻松完成的某些事情需要使用通用lambda来完成,或者根本无法完成。

Generic lambdas with decltype usage in their body are likely to be the most prominent clients of the new features. 体内使用decltype通用lambda可能是新功能的最主要客户。 And replacing the decltype / decay_t -trickery with <typename T> seems more acceptable to me in terms of additional typing and conciseness. 就附加的键入和简洁性而言,用<typename T>代替decltype / decay_t -trickery对于我来说似乎更可接受。

If you simply write 如果你只是写

[]<T>(){ }

what is T ? 什么是T

A typename ? typename An auto value? auto值?

It seems to me that, in lambdas as in common functions, it's necessary explicit what is a template argument. 在我看来,在lambda中,就像在通用函数中一样,有必要明确说明什么是模板参数。

And if you can deduce that T is a type from the use (if it's used as first template argument for std::vector must be a type) why simplify only lambda syntax and not also the syntax of traditional template functions? 并且,如果可以从用途中推断出T是类型(如果将其用作std::vector第一个模板参数,则必须是类型),为什么只简化lambda语法,而不简化传统模板函数的语法?

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

相关问题 在 C++20 中使用 typename 需要 / 概念? - Using typename in C++20 requires / concept? 为什么在 C++20 中的这种情况下仍然需要 `typename` 前缀? - Why is `typename` prefix still required in such a case in C++20? 具有非类型模板参数的 C++20 lambda - C++20 lambdas with non type template parameters 将 lambda 作为模板参数传递? (c++20,未评估上下文中的 lambdas) - Passing lambda as a template parameter? ( c++20, lambdas in unevaluated context) 在未评估的语境中的lambdas(直到C ++ 20) - lambdas in unevaluated contexts (Until C++20) 为什么我不需要在 C++20 中的依赖类型之前指定“typename”? - Why don't I need to specify “typename” before a dependent type in C++20? C++20“熟悉的模板”lambdas:在函数指针转换中指定一个显式参数 - C++20 'familiar template' lambdas: specifying an explicit argument in function pointer conversion 为什么在约束中找到模板而不在函数中找到? C++20 - Why is the template found in the constraint but not the function? C++20 C++20 约束 - 将类型名 TFunction 限制为函数(具有特定签名) - C++20 Constraints - Restrict typename TFunction to be a function (with certain signature) 是否应该在 c++20 中禁止有关缺少类型名的警告? - Should warnings about missing typename supressed in c++20?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM