应返回类型没用类型限定符被使用,为了清楚? [英] Should useless type qualifiers on return types be used, for clarity?

查看:244
本文介绍了应返回类型没用类型限定符被使用,为了清楚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的静态分析工具,抱怨了关于返回类型无用的类型限定词当我们在头文件中的原型,如:

Our static analysis tool complains about a "useless type qualifier on return type" when we have prototypes in header files such as:

const int foo();

我们将它定义这种方式,因为该函数返回永远不变的常数,以为API似乎与到位常量清晰。

We defined it this way because the function is returning a constant that will never change, thinking that the API seemed clearer with const in place.

我喜欢这种感觉类似于明确初始化的全局变量至零为清楚起见,即使C标准已经规定,如果没有明确的初始化所有全局将被初始化为零。在一天结束的时候,它其实并不重要。 (不过,静态分析工具不会抱怨的。)

I feel like this is similar to explicitly initializing global variables to zero for clarity, even though the C standard already states that all globals will be initialized to zero if not explicitly initialized. At the end of the day, it really doesn't matter. (But the static analysis tool doesn't complain about that.)

我的问题是,没有任何理由,这可能会导致一个问题?我们应该忽略由工具产生的错误,还是应该在一个不太明确的,一贯的API的成本安抚工具? (它返回其他为const char * 常量,该工具不会有问题。)

My question is, is there any reason that this could cause a problem? Should we ignore the errors generated by the tool, or should we placate the tool at the possible cost of a less clear and consistent API? (It returns other const char* constants that the tool doesn't have a problem with.)

推荐答案

这是通常的code,以尽可能准确地是怎么回事形容好。你得到这个警告,因为常量 const int的富(); 基本上是没有意义的。该API只是看起来,如果你不知道什么常量关键字手段更加清晰。不要超载的意思那样; 静态是够糟糕的,因为它是,而且也没有理由增加潜力较为混乱。

It's usually better for your code to describe as accurately as possible what's going on. You're getting this warning because the const in const int foo(); is basically meaningless. The API only seems clearer if you don't know what the const keyword means. Don't overload meaning like that; static is bad enough as it is, and there's no reason to add the potential for more confusion.

为const char * 指除 const int的不同的东西做,这就是为什么你的工具不抱怨它。前者是一个指向字符串常量,这意味着任何code调用该函数返回该类型不应该试图修改字符串的内容(这可能是在ROM为例)。在后一种情况下,系统没有办法强制执行,你不更改返回 INT ,所以预选赛是没有意义的。并行接近返回类型是:

const char * means something different than const int does, which is why your tool doesn't complain about it. The former is a pointer to a constant string, meaning any code calling the function returning that type shouldn't try to modify the contents of the string (it might be in ROM for example). In the latter case, the system has no way to enforce that you not make changes to the returned int, so the qualifier is meaningless. A closer parallel to the return types would be:

const int foo();
char * const foo2();

这都将导致您的静态分析给予警告 - 增加一个const修饰词来一回值是一个毫无意义的操作。它才有意义,当你有一个参考参数(或返回类型),比如你的为const char * 的例子。

其实,我只是做了一个小的测试程序,GCC甚至明确警告有关此问题:

In fact, I just made a little test program, and GCC even explicitly warns about this problem:

test.c:6: warning: type qualifiers ignored on function return type

所以它不是在抱怨只是你的静态分析程序。

So it's not just your static analysis program that's complaining.

这篇关于应返回类型没用类型限定符被使用,为了清楚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆