0

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Fireborn
  • 11
  • 3
  • __declspec ain't official c++, it's an extension of Microsoft. I would have never guessed to put it there, is it documented that this should work? – JVApen Apr 23 '19 at 06:36

2 Answers2

1

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! :)

Fireborn
  • 11
  • 3
0

It works in VC++ 2019, so may just be a regression in 2017?

robthebloke
  • 9,331
  • 9
  • 12