【发布时间】:2018-08-25 20:50:00
【问题描述】:
最近的 Visual Studio Studio 升级到 15.6.2 版包含一个 Visual C++ 编译器更新,由于[[nodiscard]],导致push_back 行中的以下代码出现警告:
#include <vector>
struct [[nodiscard]] S
{
int i;
};
int main()
{
std::vector<S> v;
v.push_back({ 1 }); // causes warning C4834
}
编译器是这样调用的(注意,重现不需要指定高警告级别,但/std:c++latest是必需的,/permissive-是可选的):
cl /nologo /EHsc /permissive- /std:c++latest test.cpp
警告来自 Visual C++ 自己的std::vector-实现代码并说:
warning C4834: discarding return value of function with 'nodiscard' attribute
(完整的警告输出见下文)
编译器版本:
Microsoft (R) C/C++ Optimizing Compiler Version 19.13.26128 for x64
我的理论是这个警告是由以下原因引起的:
- Visual C++ 在
emplace_back方面实现push_back, -
emplace_back在 C++17 中返回一个引用,并且 - 如果设置了
_HAS_CXX17宏,在 Visual C++ 中后一个函数的实现有点奇怪(对我来说)。
但是,不管任何内部库代码如何,Visual C++ 在生成这些诊断消息时是否违反了标准? wandbox.org 上的最新 Clang 和 GCC 版本不会对相同的代码产生任何警告。
我坚持认为 nodiscard 用户类型(如 S)的库内部使用不应引起警告,因为这会使该功能在实践中无法使用,但在 §10.6.7/2 中对 nodiscard 的简短描述[dcl.attr.nodiscard] 在那个话题上有点含糊。
或者标准只是简单地“阻止”此类警告,而这确实是一个 QoI 问题,尽管在这种情况下是一个相当严重的问题,严重到应该将其作为错误报告提交给 Microsoft?
这是完整的警告:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include\vector(996): warning C4834: discarding return value of function with 'nodiscard' attribute
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include\vector(995): note: while compiling class template member function 'void std::vector<S,std::allocator<_Ty>>::push_back(_Ty &&)'
with
[
_Ty=S
]
test.cpp(11): note: see reference to function template instantiation 'void std::vector<S,std::allocator<_Ty>>::push_back(_Ty &&)' being compiled
with
[
_Ty=S
]
test.cpp(10): note: see reference to class template instantiation 'std::vector<S,std::allocator<_Ty>>' being compiled
with
[
_Ty=S
]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include\vector(1934): warning C4834: discarding return value of function with 'nodiscard' attribute
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include\vector(1933): note: while compiling class template member function 'void std::vector<S,std::allocator<_Ty>>::_Umove_if_noexcept1(S *,S *,S *,std::true_type)'
with
[
_Ty=S
]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include\vector(1944): note: see reference to function template instantiation 'void std::vector<S,std::allocator<_Ty>>::_Umove_if_noexcept1(S *,S *,S *,std::true_type)' being compiled
with
[
_Ty=S
]
【问题讨论】:
-
返回引用不足以在
[[nodiscard]]Demo 上触发警告,所以emplace_back因为C++17 理论已经过时了。一定是发生了其他事情。 -
@AndyG:好好睡一觉之后再想一想,我认为这正是问题所在。 Visual C++ 现在会在非成员函数和成员函数(特别是复制赋值运算符...)中为引用返回类型生成这些警告。伙计,太糟糕了。我感觉微软根本不认为
[[nodiscard]]非常重要,否则这应该破坏了一些回归测试。 -
这令人失望。您是否使用我提供的演示代码进行了测试? IIRC 标准实际上并不清楚警告是否应适用于引用类型,尽管我同意 gcc 和 clang
-
@AndyG:是的,
return_self不幸地产生了与return_copy和/std:c++latest /W3相同的警告。不过,有趣的是,它确实需要/W3标志,这与我的问题中的代码相反,无论任何警告标志如何都会产生警告。 -
没有禁止警告之类的东西。如果编译器生成了一个正常运行的程序,那么它就是合规的。
标签: c++ visual-c++ c++17 language-lawyer nodiscard