需要在函数前面加上 (void) 前缀

发布于 2024-09-05 17:44:07 字数 220 浏览 6 评论 0原文

我最近遇到了一种相当不寻常的编码约定,其中对返回“void”的函数的调用以 (void) 为前缀。

例如,

(void) MyFunction();  

它与函数调用有什么不同,例如:

MyFunction();  

它有任何优势吗?还是它是另一个不必要的但有某种编码约定?

I recently came across a rather unusual coding convention wherein the call for a function returning "void" is prefixed with (void).

e.g.

(void) MyFunction();  

Is it any different from the function call like:

MyFunction();  

Has it got any advantage or is it yet another needless but there coding convention of some sort?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

近箐 2024-09-12 17:44:07

某些函数(如 printf())返回一个在实际代码中几乎从未使用过的值(对于 printf 来说,是打印的字符数)。然而,一些工具,如lint,期望如果一个函数返回一个值,则必须使用它,并且会抱怨,除非您编写如下内容:

int n = printf( "hello" );

使用void强制转换:

(void) printf( "hello" );

是告诉此类工具您的一种方式真的不想使用返回值,因此让他们保持安静。如果您不使用此类工具,则无需费心,并且在任何情况下,大多数工具都允许您将它们配置为忽略特定函数的返回值。

Some functions like printf() return a value that is almost never used in real code (in the case of printf, the number of characters printed). However, some tools, like lint, expect that if a function returns a value it must be used, and will complain unless you write something like:

int n = printf( "hello" );

using the void cast:

(void) printf( "hello" );

is a way of telling such tools you really don't want to use the return value, thus keeping them quiet. If you don't use such tools, you don't need to bother, and in any case most tools allow you to configure them to ignore return values from specific functions.

星軌x 2024-09-12 17:44:07

不,没有任何区别——被转换为 void 的是函数的返回值。

我想说这可能是有意义的,您想明确表示您没有使用返回值(您调用它是为了产生副作用),但由于该函数已经具有 void return,所以它没有多大意义。

No, there isn't any difference -- what's being cast to void is the return value of the function.

I'd say it could make sense of you wanted to make explicit you're not using the return value (you're calling it for the side effects), but as the function already has void return, it doesn't make much sense.

旧街凉风 2024-09-12 17:44:07

如果函数返回一些内容,则 void 可以避免(!)在某些编译器(或 lint 工具)上出现警告(实际上我无法让 gcc 警告我返回值丢失);但更重要的是,它清楚地表明返回值是故意“丢弃”的(而不是错误地)。

If the function returns something the void could avoid (!) a warning (indeed in no way I was able to make gcc warn me that the return value is lost) on some compilers (or lint tools); but more importantly, it makes clear that a return value is "thrown" away purposely (and not by mistake).

小鸟爱天空丶 2024-09-12 17:44:07

学术上:“函数”总是返回一些东西,否则它将是一个过程。所以这段代码的作者想说“我知道这个命名是错误的,但我不会更改名称,所以我让这个干扰可见”

acedemically: a "function" always returns something, else it would be a procedure. So the Author of this code wants to say "i know this naming is wrong, but i will no change the name, so i make this disturbance visible"

放飞的风筝 2024-09-12 17:44:07

它有什么优势吗?还是又一个不必要的东西,但有某种编码约定?

没有区别。这是一个非常常见的约定,例如在软件测试中,以强调这样一个事实:在上下文中,函数返回(如果有)可以安全地被丢弃。

Has it got any advantage or is it yet another needless but there coding convention of some sort?

No difference. It is a quite common convention e.g. in software testing to highlight the fact that in context the function return, if any, is safe to be discarded.

机场等船 2024-09-12 17:44:07

在 HPUX 手册页中,示例代码中经常会看到强制转换为 void 来绕过 lint 警告。

fprintf(mystream, "%s\n", "foo");

vs.

(void)fprintf(mystream, "%s\n", "foo");

这可能是代码作者的来源。 IMO,这不是一个好主意,因为大多数 sprintf 系列都调用 malloc。当内存不足时malloc会失败。对于 printf() 系列成员,SIGINT 还会导致底层 write() 系统调用中断并且不会写入所有缓冲区。

In HPUX man pages it is quite common in example code to see a cast to void to get around lint warnings.

fprintf(mystream, "%s\n", "foo");

vs.

(void)fprintf(mystream, "%s\n", "foo");

That may be where the author of the code is coming from. IMO, this is not a great idea because most of the sprintf family, for example, call malloc. malloc will fail when there is not enough memory. SIGINT also causes the underlying write() syscall to interrupt and not write all of the buffer, for printf() family members.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文