Lambda表达式作为类模板参数在C ++ 14中 [英] Lambda expressions as class template parameters in C++14

查看:1013
本文介绍了Lambda表达式作为类模板参数在C ++ 14中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 Lambda表达式作为类模板参数询问了以下问题的可能性:使用lambda表达式作为类模板参数。



问题的答案是否定的。但是,它是关于C ++ 11。



新标准C ++ 14中的情况有没有改变?

解决方案

没有C ++ 14的情况没有改变,事实上 5.1.2 段落已经从

缩写为


lambda表达式不出现在未经评估的操作数中(条款
5)。


至:



[...] lambda表达式不会出现在未求值的操作数中
(第5条),在模板中,在别名声明中,在
typedef声明,或者在函数或函数声明中
模板的函数体和默认参数之外。 [注:
的目的是防止lambdas出现在签名中。 -end
note] [...]


缺陷报告1607.模板参数中的Lambdas 导致此更改。



缺陷报告仅仅倾向于讨论不允许这样做的理由,但我们可以找到一个非常详细的解释,说明为什么在未经评估的上下文中不允许使用lambda表达式的理由。原因归结为:




  • Lambda表达式没有唯一类型

  • 编译器实现问题:

    • 例如 SFINAE
    • 的非凡扩展
    • 可能需要命名整个lambda的mangle。




这个限制的原因似乎不太可能改变。 >

The question Lambda expressions as class template parameters asks about the possibility of using lambda expressions as class template parameters.

The answer to the question was no. However, it was about C++11.

Has the situation changed in the new standard, C++14?

解决方案

No the situation in C++14 has not changed at all and in fact the language in section 5.1.2 Lambda expressions paragraph 2 has been tightened from:

A lambda-expression shall not appear in an unevaluated operand (Clause 5).

to:

[...]A lambda-expression shall not appear in an unevaluated operand (Clause 5), in a templateargument, in an alias-declaration, in a typedef declaration, or in the declaration of a function or function template outside its function body and default arguments. [ Note: The intention is to prevent lambdas from appearing in a signature. —end note ][...]

Defect report 1607. Lambdas in template parameters lead to this change.

The defect report only obliquely deals with the rationale for disallowing this but we can find a very detailed explanation for why this is disallowed in Rationale for lambda-expressions not being allowed in unevaluated contexts. The reasons boil down to:

  • Lambda expressions not having a unique type
  • Compiler implementation issues:
    • Such as an extraordinary expansion of SFINAE
    • The possible requirement to name mangle the whole body of a lambda.

Given the rationale for this restriction it seems unlikely to change.

这篇关于Lambda表达式作为类模板参数在C ++ 14中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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