lambdas中的模板参数列表中的auto是否是标准的一部分? [英] Is auto in template parameter list in lambdas part of the standard?

查看:70
本文介绍了lambdas中的模板参数列表中的auto是否是标准的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我偶然发现了以下代码片段:

Today, I stumbled across the following code snippet:

#include <utility>

int main()
{

  auto a = [](std::pair<auto, auto> value)
  {

  };

  a(std::pair<int, bool>{ 3, true });
}

http://cpp.sh/5p34

我只有一个问题:该代码受标准支持吗?

I have only one question: is this code supported by the standard?

它可以在GCC(带有-std=c++14)中进行编译,但不能在clang或Visual Studio 2015(VC ++ 14)中进行编译.

It compiles in GCC (with -std=c++14), but not clang or Visual Studio 2015 (VC++14).

这似乎应该成为标准的一部分,因为如果lambda与常规函数应具有相同的模板支持,则应予以支持.

This seems like it should be part of the standard because if lambdas should have the same template support as regular functions, then this should be supported.

这似乎可以转换为所有模板类型,而不仅仅是std::pair.

This seems to convert to all template types, not just std::pair.

推荐答案

在C ++ 14中,模板参数(无论是否为lambda)均不允许使用auto. Clang和Visual Studio都可以拒绝此代码.

In C++14, auto is not allowed in template arguments, whether in a lambda or not. Clang and Visual Studio are both right to reject this code.

C ++ 14标准参考为[dcl.spec.auto].在以下情况下允许使用auto说明符:

The C++14 standard reference is [dcl.spec.auto]. The auto specifier is allowed in the following contexts:

  • 在函数声明器的 decl-specifier-seq 中(例如auto f();)(第2段)
  • conversion-function-id 中(一个类中的operator auto())(第2段)
  • 在函数声明符(例如auto f() -> auto;)的追溯返回类型中(第2段)
  • 在lambda的 parameter-declaration (作为 decl-specifiers 之一)的 decl-specifier-seq 中3); -这就是允许通用lambda存在的原因-
  • 在块范围或名称空间范围的变量声明中(第4段)
  • 在for循环控制变量(第4段)的声明中,包括基于范围的for循环(第5段)
  • ifswitch语句或循环的情况下(第5段)
  • new表达式中, new auto(42)(第5段)
  • 在类的定义中声明静态数据成员(第5段)
  • In the decl-specifier-seq of a function declarator (e.g., auto f();) (paragraph 2)
  • In a conversion-function-id (i.e., an operator auto() in a class) (paragraph 2)
  • In the trailing-return-type of a function declarator (e.g., auto f() -> auto;) (paragraph 2)
  • In the decl-specifier-seq of a parameter-declaration (as one of the decl-specifiers) of a lambda (paragraph 3); -this is what allows generic lambdas to exist-
  • In the declaration of a variable at block scope or namespace scope (paragraph 4)
  • In the declaration of a for loop control variable (paragraph 4), including a range-based for loop (paragraph 5)
  • In the condition of an if or switch statement or a loop (paragraph 5)
  • In a new expression, i.e., new auto(42) (paragraph 5)
  • In the declaration of a static data member in the definition of a class (paragraph 5)

最后,

在本节未明确允许的上下文中使用autodecltype(auto)的程序格式错误.

A program that uses auto or decltype(auto) in a context not explicitly allowed in this section is ill-formed.

因此,模板参数中不允许使用auto,因为[dcl.spec.auto]中没有枚举这种情况.

Therefore, auto is not allowed in template parameters, since that case isn't enumerated in [dcl.spec.auto].

我不知道为什么gcc允许它.它可能与Concepts Lite有关,但我不知道Concepts Lite是否实际上允许这种用法.它可能只是易于执行的无关扩展.我认为

I don't know why gcc allows it. It might be related to Concepts Lite, but I don't know if Concepts Lite actually allows this usage. It could just be an unrelated extension that is easy to implement. I assume that

[](std::pair<auto, auto> value) { /* ... */ }

被翻译成

struct __some_unique_name {
    template <typename T1, typename T2>
    auto operator()(std::pair<T1, T2> value) const { /* ... */ }
    // ...
};

这篇关于lambdas中的模板参数列表中的auto是否是标准的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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