英特尔C ++编译器,当非const参数作为常量参数传递警告167 [英] Intel C++ Compiler warning 167 when non-const argument is passed as const parameter
问题描述
我有一个大codeBase的,最近从微软的编译器的英特尔C ++编译感动。我们团队的目标是无主线警告编译。由于开关,警告167的一个实例已经困惑了我。如果我编译以下code:
I have a large codebase that recently moved from Microsoft's compiler to the Intel C++ Compiler. Our team's goal is compilation without warnings in the mainline. Since the switch, one instance of warning 167 has confounded me. If I compile the following code:
int foo(const int pp_stuff[2][2])
{
return 0;
}
int foo2(const int pp_stuff[][2])
{
return 0;
}
int main(void)
{
int stuff[2][2] = {{1,2},{3,4}};
foo(stuff);
foo2(stuff);
return 0;
}
国际刑事法院会给我警告:
The ICC will give me warnings:
1>main.c(17): warning #167: argument of type "int (*)[2]" is incompatible with parameter of type "const int (*)[2]"
1> foo(stuff);
1> ^
1>
1>main.c(18): warning #167: argument of type "int (*)[2]" is incompatible with parameter of type "const int (*)[2]"
1> foo2(stuff);
为什么这应该是一个警告?通常的作法是通过一个非const变量作为常量参数和类型&放大器;尺寸是相同的。
Why should this be a warning? It is common practice to pass a non-const variable as a const parameter, and the types & dimensions are identical.
对于那些谁标志着这个重复的问题,我劝你三思。如果别人遇到这样的警告,他们必须知道,在C参数在原型的功能转换,如果通过转让,然后搜索一个问题,是关于严格分配。尽管答案最终被从C90 / C99相同的条款,问题是,我认为,pretty不同。
To those who have marked this a duplicate question, I urge you to reconsider. If someone else encounters this warning, they would have to know that in C arguments are converted as if by assignment in prototyped functions, and then search for a question that is strictly about assignment. Even though the answer ends up being the same clause from C90/C99, the question is, I think, pretty different.
推荐答案
你要把你的变量,常量,当你把它传递到需要常量的功能。
Cast your variable as const when you pass it to the function that requires const.
foo( (const int (*)[2]) stuff );
为什么我不能传递一个char **到期望一个const char功能** ?
<一个href=\"http://stackoverflow.com/questions/14901183/intel-c-compiler-warning-167-when-non-const-argument-is-passed-as-const-parame\">Similar问题
这篇关于英特尔C ++编译器,当非const参数作为常量参数传递警告167的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!