Search code examples
c++templatesvisual-c++lambdadeclspec

__declspec(noinline) on lambda function inside template class throws syntax error in VS2017


While trying to debug an issue with an assert macro I came across this problem. Using __declspec(noinline) on a lambda function in a template class generates a syntax warning in Visual Studio 2017:

error C2760: syntax error: unexpected token '__declspec', expected '{'

This is the failing code:

template<class R>
class test
{
public:
    void DoStuff()
    {
        []() __declspec(noinline) { }; // syntax error
    }
};

int WinMain(void)
{
    return 0;
}

If I go to my project settings and switch my platform toolset from v141 (vs2017) to v140 (vs2015) in the general section of the project properties dialog the error goes away .

If I change the class to not be a template class it also compiles correctly:

class test
{
public:
    void DoStuff()
    {
        []() __declspec(noinline) { }; // compiles fine
    }
};

int WinMain(void)
{
    return 0;
}

I'm curious why this wouldn't succeed using the v141 platform toolset. Is there some other project setting that could be affecting this?


Solution

  • I was able to fix this by updating Visual Studio 2017 to the latest version (15.9.7). Previously I was running version 15.6.7. Thanks to everyone who looked in and commented! :)