VS2013 Intellisense doesn't understand decltyp

2019-05-20 05:17发布

问题:

Is there a patch out there (official or unofficial) to get IntelliSense to stop reporting every use of decltype as a syntax error? It compiles fine, so I know decltype is supported, but it's very distracting seeing red squiggles everywhere and makes it much harder to find actual errors in the code. Every compile gives me a list of hundreds of non-errors - basically at least 3 for every use of decltype in the code base, e.g.:

std::for_each(std::begin(list), std::end(list), [](const decltype(list)::value_type& item)
{
    <do stuff with item>
});

will produce the following (non) errors:

IntelliSense: global-scope qualifier (leading '::') is not allowed
IntelliSense: expected a ')'
IntelliSense: identifier "item" is undefined

Upgrading to VS2015 is not an option at this point. (I doubt I could convince the company to shell out to upgrade every computer, and upgrading only some of them would lead to backwards compatibility issues.)

Personally, I'd prefer not to use decltype at all until we get an IDE that fully supports it (there's nowhere I know of that you actually need it), but I don't think I can convince everybody of that either. I just want to make all those fake errors go away so that I can find the real ones without poring over thousands of false-positives.

回答1:

Given a helper template alias

template <typename T> using id = T;

you can avoid the Intellisense errors, while still keeping the code perfectly valid, by writing id<decltype(list)>::value_type where you would otherwise have written decltype(list)::value_type.

Depending on how often decltype is immediately followed by ::, you may want to create a macro as simple as:

#define DECLTYPE(x) id<decltype(x)>