rafalc
rafalc

Reputation: 202

Generic lambda in extern "C" function

Following code is accepted by gcc 6 and clang 4, but MSVC 2017 which claims to support C++14 (and generic lambdas in particular) discards it with error C2894: templates cannot be declared to have 'C' linkage

extern "C" void f() {
    std::vector<int> v { 1, 2, 3 };
    std::for_each(std::begin(v), std::end(v), [](auto& x) {
        x++;
    });
    std::cout << v[0];
}

I understand that generic lambda is internally translated to some structure with templated call-operator but how does it interfere with extern "C"?

Is it a bug in MSVC? Can you suggest a workaround for using generic lambdas in extern "C" functions?

UPDATE

I reported this issue to Microsoft and the response was

Thank you for your feedback! We have determined that this issue is not a bug. See msdn https://msdn.microsoft.com/en-us/library/95bhc9c2.aspx

which doesn't explain much.

Upvotes: 4

Views: 398

Answers (0)

Related Questions