Visual C++ 2015 在使用 decltype(auto) 时给出 C4552 警告

Visual C++ 2015 gives C4552 warning when used decltype( auto )

我想我在 Visual C++ 2015 中遇到了一个错误,但我想确定一下。考虑这个片段:

template < typename T >
decltype( auto ) f( T param )
{
    return param + 1;
}

int main()
{
    auto i = f( 10 );
    return 0;
}

Visual C++ 2015 在 return 语句中给出此警告:

warning C4552: '+': operator has no effect; expected operator with side-effect

尽管它似乎对生成的代码没有任何影响。这是编译器错误吗?

按照微软自己的说法here,可能是"mistake"。尝试:

decltype(auto) f(T param)
{
    return (param + 1);
}

或者只是压制这场战争,因为微软可能不会以他们的 "mistake" 借口修复它。

这似乎是一个错误。它在这里有一个开放的错误报告:

https://connect.microsoft.com/VisualStudio/feedback/details/1468881/decltype-auto-causes-unnecessary-warnings

正如 already, the observed behavior has a pending Microsoft Connect report 中指出的那样。该问题尚未得到评估。

要解决此问题1),您可以在表达式两边加上括号:

template < typename T >
decltype( auto ) f( T param )
{
    return ( param + 1 );
}


1) 已使用 Visual Studio 2015 社区版进行验证