0

PVS Studio 6.17 (Windows 7, 64Bit, VS2017, C++-03) 似乎在遵循精简代码时给出错误警告

#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' 变量可以在较低级别的范围内构造。

由于sstd::stack-type,而对应的算法要求rhs-element从栈中弹出,看来PVS-Studio是错误的。我错过了什么?

顺便一提:

PVS Studio 消息中有错字:

   perfomance->performance

参考

4

1 回答 1

2

在评论中讨论了代码优化的方法。是的,它可以优化,但我认为它实际上没有意义。如果一定要使用C++-03,那么由于优化,代码会变得复杂难懂,不好。好吧,当然,使用 std::move 是合适的。

现在说一下 PVS-Studio。分析仪不对,这里发出警告。在-scoperhs内创建并重新定位变量是不可能的。if分析器没有考虑到数据源会改变并s.top()返回另一个值。好吧,V821诊断是新的,有缺点。我们将尝试消除这种误报。感谢您提供的示例,以及有关拼写错误“性能”一词的信息。

于 2017-09-06T08:50:30.673 回答