在我当前使用 MISRA 2004 标准的项目中,我们使用三个 GCC 编译器,版本 3.2.3、4.4.2 和 5.4.0。
我们使用 pedantic switch 和 c89 标准以及其他一些限制来运行构建检查。限制之一是所有数据必须在声明时初始化。
我有一个问题,在 GCC 3.2.3 上,通用零初始值设定项{0}只编译基本单一类型的数组。如果我有一个结构数组,那么我会收到一个缺少大括号的警告,并且只有当我更改{0}为{{0}}.
struct my_type my_thing[NUMBER_OF_THINGS] = {0};
Run Code Online (Sandbox Code Playgroud)
变成
struct my_type my_thing[NUMBER_OF_THINGS] = {{0}};
Run Code Online (Sandbox Code Playgroud)
但是,这对于具有结构成员的结构数组不起作用。然后问题出在 4.4.2 编译器上,它会导致缺少初始化程序错误,所以我必须这样做:
struct my_struct_with_structs_inside my_other_thing[NUMBER_OF_THINGS] = {{0, 0, {0}, 0}};
Run Code Online (Sandbox Code Playgroud)
这满足编译器的要求,但它会触发我们的 MISRA 检查器,因为 MISRA 需要通用单个 {0} 初始化程序或完整的全数组初始化程序:
struct my_struct_with_structs_inside my_other_thing[NUMBER_OF_THINGS] = {{0, 0, {0}, 0},
{0, 0, {0}, 0},
{0, 0, {0}, 0},
{0, 0, {0}, 0},
{0, 0, {0}, 0}};
Run Code Online (Sandbox Code Playgroud)
这对我们来说是不切实际的,因为我们有各种各样的限制,并且NUMBER_OF_THINGS …
我正在编写一个程序,它在命令行上接受数字类型,然后使用该类型调用通用函数。例如,我这样运行程序:
my_programme u32
Run Code Online (Sandbox Code Playgroud)
...在我的代码中,我有:
my_programme u32
Run Code Online (Sandbox Code Playgroud)
如果我想对 8、16、32 和 64 位整数(有符号和无符号)执行此操作,则需要大量调用my_generic_function(). 它看起来凌乱,似乎没有必要。
我可以在&stror Stringvalues 和 type之间定义一个映射T,或者编写一个函数来返回 type T,而不是 match 语句,我可以只写:
match cli_type
{
"u32" =>
{
my_generic_function::<u32>(args);
},
"u16" =>
...
Run Code Online (Sandbox Code Playgroud)