首页 > 解决方案 > 通用 lambda 尾随返回类型中的函数范围名称无法编译 (MSVC)

问题描述

给定以下代码片段,案例 1、2、3 和 4 在 Visual Studio 2017 (/std:c++14) 中编译,但案例 5 没有。这是怎么回事?

int i;
auto case1 = [](auto) -> decltype(i, void()) {};

int main() {
    int j;
    auto case2 = [](int) -> decltype(j, void()) {};
    auto case3 = [j](auto) -> decltype(j, void()) {};
    auto case4 = [](auto) -> decltype(i, void()) {};
    auto case5 = [](auto) -> decltype(j, void()) {};

    case1(int()); // OK
    case2(int()); // OK
    case3(int()); // OK
    case4(int()); // OK
    case5(int()); // Error: message below
    return 0;
}

这是编译器的输出。

main.cpp(15): error C2672: 'operator __surrogate_func': no matching overloaded function found 
main.cpp(15): error C2893: Failed to specialize function template 'void main::<lambda_1>::operator ()(_T1) const' 
main.cpp(15): note: With the following template arguments: '_T1=int'

标签: visual-c++c++14trailing-return-typegeneric-lambda

解决方案


这是 MSVC 中的一个错误。我打开了一个错误:错误实例化 lambda 模板 修复由赞成票优先。随意赞成这个问题。


推荐阅读