PVS工作室6.17来构造(Windows 7中,64位,VS2017,C++ - 03)似乎给一个错误警告上以下降为代码错误肯定警告PVS Studio:V821性能下降。在“RHS”变量可以在一个较低的水平范围
#include <stack>
#include <string>
#include <vector>
bool fred(const std::string &x)
{
return x == "ab";
}
std::vector<std::string> bar(std::stack<std::string> & s)
{
std::vector<std::string> v;
const std::string rhs(s.top()); // V821 Decreased perfomance. The 'rhs' variable can be constructed in a lower level scope.
s.pop();
const std::string lhs(s.top());
s.pop();
if (fred(lhs))
{
v.push_back(rhs);
}
return v;
}
从该警告PVS工作室是
V821性能下降。 'rhs'变量可以在较低级别的范围内构建。
由于s
是std::stack
类型,并且相应的算法要求从堆栈中弹出rhs
-element,所以它看起来像PVS-Studio是错误的。我错过了什么?
顺便说一句:
有一个在PVS工作室消息一个错字:
perfomance->performance
参考
也许PVS正在讨论'rhs'开始只有在'fred(lhs)'为真时才使用。即使你不能改变(你真的不能吗?)我会认为这是一个真正的积极的 – user463035818
我不确定你希望从中得到什么样的答案。您可能会稍微重新排列代码,并可能会更改警告,但最终可能并不重要。甚至他们的文件谈判的误报。 https://www.viva64.com/en/w/V821/ –
你可以std ::移动rhs和lhs,因为你之后立即弹出它们。它会避免复制。 –