且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 void 双指针类型转换为 void,为什么?

更新时间:2022-06-27 00:50:16

回调的类型可能是 LIBSSH2 库 API 的一部分.库将每个参数传递给它期望回调需要的回调.无论那个参数是什么,这个特定的回调都不需要它.程序员有四种选择:

The type of the callback is probably part of the API of a LIBSSH2 library. The library passes every parameter to the callback that it expects the callback will need. Whatever that parameter is, this particular callback doesn't need it. The programmer has four choices:

  1. 他可以省略原型中的参数名称,将void **abstract 替换为void **.这使得试图理解他的代码的人必须查看 LIBSSH2 API 才能理解最后一个参数是什么.

  1. He can leave out the name of the parameter in the prototype, replacing void **abstract with void **. This makes it so that someone trying to understand his code has to look at the LIBSSH2 API to understand what the last parameter is.

他不能使用参数.但这会让他的编译器发出警告.

He can just not use the parameter. But this will get him a warning from his compiler.

他可以以不会隐藏警告的方式使用参数.

He can use the parameter in a way that has no consequence to hide the warning.

他可以注释掉参数名,像这样:void **/*abstract*/.

He could comment out the parameter name, like this: void ** /*abstract*/.

该程序员选择选项 3.

This programmer choose option 3.

就我个人而言,对于这种情况,我倾向于选择选项 4.我也希望看到这样的内容:

Personally, I tend to prefer option 4 for this case. I'd also prefer to see something like this:

#define OK_UNUSED(a) if (false && (a)); else (void) 0

...

OK_UNUSED(abstract);

这清楚地表明,不使用参数是可以的.

This makes it very clear that it's okay that the parameter is unused.