c++ - 如何在不违反 MISRA C++ 2008 咨询规则 5-2-10 的情况下使用 std::transform?

标签 c++ misra

我在 PC-Lint (au-misra-cpp.lnt) 中得到这些错误:

ConverterUtil.cpp(90): error 864: (Info -- Expression involving variable 'transformValue' possibly depends on order of evaluation [MISRA C++ Rule 5-2-10])

ConverterUtil.cpp(90): error 864: (Info -- Expression involving variable 'transformValue' possibly depends on order of evaluation [MISRA C++ Rule 5-2-10])

ConverterUtil.cpp(90): error 534: (Warning -- Ignoring return value of function 'std::transform(std::_String_iterator>>, std::_String_iterator>>, std::_String_iterator>>, int (*)(int))' (compare with line 998, file C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\algorithm) [MISRA C++ Rules 0-1-7 and 8-4-6], [MISRA C++ Rule 0-3-2])

关于这段代码:

/**Conversion from std::string to bool*/
bool ConverterUtil::ConvertStdStringToBool(const std::string value)
{
    std::string transformValue = value;
    bool retValue = false;

    std::transform(transformValue.begin(), transformValue.end(), transformValue.begin(), &::tolower);


    if(transformValue == std::string(static_cast<const char *>("true")))
    {
        retValue = true;
    }

    return retValue;
}

我猜它不喜欢我在转换中使用相同的 std::string 作为输入和输出,但使用其他字符串作为输出会产生相同的错误。

是否可以使 std::transform MISRA 兼容?

最佳答案

我只是猜测(如果它不能解决您的问题,我可能会删除答案)。

尝试用这两个替换包含 std::transform 的行:

auto dest = transformValue.begin();
std::transform(transformValue.cbegin(), transformValue.cend(), dest, &::tolower);

注意 cbegin()cend() 的使用(而不是 begin()end()).

关于另一个主题:您将传递给 ConvertStdStringToBool 的字符串复制了两次,而您只能复制一次。为此,请替换:

bool ConverterUtil::ConvertStdStringToBool(const std::string value)
{
    std::string transformValue = value;

bool ConverterUtil::ConvertStdStringToBool(std::string transformValue)
{

(在此更改后,您可能希望将 transformValue 重命名为 value)。

更新:我对为什么我认为它会有所帮助的解释。

首先,请注意 transformValue 是非 const。因此,transformValue.begin()transformValue.end() 将调用这些重载:

iterator begin(); // non const overload
iterator end();   // non const overload

因此,静态分析器(正确地)得出结论,begin()end() 可能会改变 transformValue 的状态。在这种情况下,transformValue 的最终状态可能取决于首先调用 begin()end() 中的哪一个。

现在,当您调用 cbegin()cend() 时,重载如下:

const_iterator cbegin() const; // notice the const 
const_iterator cend() const;   // notice the const

在这种情况下,静态分析器不会推断这些调用会改变 transformValue 的状态,也不会引发问题。 (严格来说,即使这些方法是 const,它们也可以改变状态,因为类中可能存在 mutable 数据成员,或者这些方法可能使用邪恶的 const_cast。恕我直言,静态分析器不应该为此受到指责。)

最后的评论:电话

std::transform(transformValue.cbegin(), transformValue.cend(), transformValue.cbegin(), &::tolower);
                                                                              ^^^^^^

错了。第三个参数必须是非const迭代器,也就是说,它必须transformValue.begin() (只有前两个参数是 c* 方法)。

但是,我想,出于与上述类似的推理,仅使用 transformValue.begin() 作为第三个参数是不够的,这就是为什么我建议创建另一个变量 (目标).

关于c++ - 如何在不违反 MISRA C++ 2008 咨询规则 5-2-10 的情况下使用 std::transform?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145006/

相关文章:

c++ - 'WCHAR' 和 'wchar_t' 有什么区别?

c++ - 类成员在构造函数中和构造函数外的不同值

c - 终止带有 else 子句的 if … else if 有什么好处?

c - 如何使 (1 << 9) 通过 MISRA?

c - MISRA 相当于医疗行业?

c++ - std::endl 在每个平台上究竟代表什么?

c++ - 使用 MICO (CORBA) 从 IDL 文件生成 C++ 类

c++ - 是否可以将 VTK 中的 (polyData) 编写器用于其他库中的数据类型?

c - 参数数量有限的 printf 的替代方案?

c - 米斯拉 C : 2004: typedefs that indicate size and signedness should be used in place of the basic types