Function-scope name in generic lambda trailing return type won't compile (MSVC)

69 Views Asked by At

Given the following code snippet, case 1, 2, 3, and 4 compile in Visual Studio 2017 (/std:c++14), but case 5 doesn't. What's going on?

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;
}

Here's the compiler's output.

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'
1

There are 1 best solutions below

0
On

This is a bug in MSVC. I opened a bug: Error instantiating lambda template Fixes are prioritized by upvotes. Feel free to upvote the issue.