gpt4 book ai didi

c++ - 如何在不违反 MISRA C++ 2008 要求规则 5-2-12 的情况下在 std::map 中插入项目?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:05 31 4
gpt4 key购买 nike

我在 PC-Lint (au-misra-cpp.lnt) 中收到此错误:

error 1960: (Note -- Violates MISRA C++ 2008 Required Rule 5-2-12, Array type passed to function expecting a pointer)

关于这段代码:

_IDs["key"] = "value";

_IDs 声明为:

std::map<std::string,std::string> _IDs;

还尝试更改为:

_IDs.insert("key","value");

但得到同样的错误。

如何使代码符合 misra 标准?

最佳答案

违反的规则是调用std::string::string(const CharT* s,
const Allocator& alloc = Allocator())
, 将从 char const [] 衰减到一个字符指针。

我认为,解决方案是显式转换为指针类型:

_IDs[static_cast<char const *>("key")] = static_cast<char const *>("value");

但是,我建议不要使用(或至少升级)在您实际使用 std::string 时发出警告的 linter .

另请注意,您不能调用 std::map::insert你尝试做的方式。没有直接采用键和值的重载,而是采用由键和值组成的一对的重载。参见 here重载编号 1。

关于c++ - 如何在不违反 MISRA C++ 2008 要求规则 5-2-12 的情况下在 std::map 中插入项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16852996/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com