lambda中使用的"auto"变量在自己的初始值设定项中

Joh*_*itb 35 c++ lambda auto c++14

今天我发现了这段代码

#include <cstdio>

auto terminal = [](auto term)           
{                                       
    return [=] (auto func)             
    {                                   
        return terminal(func(term));
    };
};
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,GCC接受了它.Clang拒绝它,因为它terminal在自己的初始化器中使用并被声明auto.

我期待clang给出的错误,但它实际上是不正确的吗?或者必须接受代码?

T.C*_*.C. 15

认为这会遇到§7.1.6.4[dcl.spec.auto]/p11:

如果需要具有未减少占位符类型的实体的类型来确定表达式的类型,则该程序是不正确的.

你需要的类型terminal来确定的类型ID表达 terminalreturn terminal(func(term));(编辑,帽尖@Richard史密斯),但在那个表达的点,你不能推断的类型terminal呢.

  • apply-operator的返回类型是相关的,只有在实例化时才必须确定. (5认同)
  • 你需要`terminal`的类型来确定*id-expression*`terminal`的类型,所以程序是不正确的. (4认同)