省略参数名称,C++ vs C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8776810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
parameter name omitted, C++ vs C
提问by Alcott
In C++, I tend to omit the parameter's name under some circumstances. But in C, I got an error when I omitted the parameter's name.
在 C++ 中,我倾向于在某些情况下省略参数的名称。但是在 C 中,当我省略参数名称时出现错误。
Here is the code:
这是代码:
void foo(int); //forward-decl, it's OK to omit the parameter's name, in both C++ and C
int main()
{
foo(0);
return 0;
}
void foo(int) //definition in C, it cannot compile with gcc
{
printf("in foo\n");
}
void foo(int) //definition in C++, it can compile with g++
{
cout << "in foo" << endl;
}
Why is that? Can't I omit the parameter's name in C function definition?
这是为什么?我不能在 C 函数定义中省略参数的名称吗?
采纳答案by adl
No, in C you cannot omit identifiers for parameters in function definitions.
不,在 C 中,您不能省略函数定义中参数的标识符。
The C99 standard says:
C99 标准说:
[6.9.1.5] If the declarator includes a parameter type list, the declaration of each parameter shall include an identifier, except for the special case of a parameter list consisting of a single parameter of type void, in which case there shall not be an identifier. No declaration list shall follow.
[6.9.1.5] 如果声明符包含参数类型列表,则每个参数的声明都应包含一个标识符,但参数列表由一个 void 类型的单个参数组成的特殊情况除外,在这种情况下,不应有标识符。不得遵循申报清单。
The C++14 standard says:
C++14 标准说:
[8.3.5.11] An identifier can optionally be provided as a parameter name; if present in a function definition , it names a parameter (sometimes called “formal argument”). [Note: In particular, parameter names are also optional in function definitionsand names used for a parameter in different declarations and the definition of a function need not be the same.]
[8.3.5.11]可以选择性地提供标识符作为参数名称;如果出现在函数定义中,它会命名一个参数(有时称为“形式参数”)。[注意:特别是,参数名称在函数定义中也是可选的,不同声明中用于参数的名称和函数的定义不必相同。]
回答by Keith Thompson
The reason is that that's what the respective language standards say, but there is a rationale for the difference.
原因是这就是各自的语言标准所说的,但存在差异的原因。
If you don't provide a name for a parameter, then the function cannot refer to that parameter.
如果您没有为参数提供名称,则该函数无法引用该参数。
In C, if a function ignores one of its parameters, it usually makes sense just to remove it from the declaration and the definition, and not pass it in any calls. An exception might be a callback function, where a collection of functions all have to be of the same type but not all of them necessarily use their parameters. But that's not a very common scenario.
在 C 中,如果一个函数忽略了它的一个参数,通常只需将它从声明和定义中删除,而不是在任何调用中传递它。一个例外可能是回调函数,其中一组函数都必须具有相同的类型,但并非所有函数都必须使用它们的参数。但这不是一个很常见的场景。
In C++, if the function is derived from a function defined in some parent class, it has to have the same signature as the parent, even if the child function has no use for one of the parameter values.
在 C++ 中,如果函数派生自某个父类中定义的函数,则它必须具有与父类相同的签名,即使子函数对其中一个参数值没有用处。
(Note that this is not related to default parameters; if a parameter in C++ has a default value, the caller doesn't have to pass it explicitly, but the function definition still has to provide a name if it's going to refer to it.)
(请注意,这与默认参数无关;如果 C++ 中的参数具有默认值,则调用者不必显式传递它,但如果要引用它,函数定义仍然必须提供名称。 )
回答by Joseph Quinsey
On a purely practical level, I have deal with this daily. The best solution to date is to use the pre-processor. My common header file contains:
在纯粹的实践层面上,我每天都在处理这个问题。迄今为止最好的解决方案是使用预处理器。我的通用头文件包含:
//-------------------------------------------------------------------------
// Suppress nuisance compiler warnings. Yes, each compiler can already
// do this, each differently! VC9 has its UNREFERENCED_PARAMETER(),
// which is almost the same as the SUPPRESS_UNUSED_WARNING() below.
//
// We append _UNUSED to the variable name, because the dumb gcc compiler
// doesn't bother to tell you if you erroneously _use_ something flagged
// with __attribute__((unused)). So we are forced to *mangle* the name.
//-------------------------------------------------------------------------
#if defined(__cplusplus)
#define UNUSED(x) // = nothing
#elif defined(__GNUC__)
#define UNUSED(x) x##_UNUSED __attribute__((unused))
#else
#define UNUSED(x) x##_UNUSED
#endif
An example of the use of UNUSED is:
使用 UNUSED 的一个例子是:
void foo(int UNUSED(bar)) {}
Sometimes you actually need to refer to the parameter, for example in an assert() or debug statement. You can do so via:
有时您实际上需要引用参数,例如在 assert() 或 debug 语句中。您可以通过:
#define USED_UNUSED(x) x##_UNUSED // for assert(), debug, etc
Also, the following are useful:
此外,以下内容也很有用:
#define UNUSED_FUNCTION(x) inline static x##_UNUSED // "inline" for GCC warning
#define SUPPRESS_UNUSED_WARNING(x) (void)(x) // cf. MSVC UNREFERENCED_PARAMETER
Examples:
例子:
UNUSED_FUNCTION(int myFunction)(int myArg) { ...etc... }
and:
和:
void foo(int bar) {
#ifdef XXX
// ... (some code using bar)
#else
SUPPRESS_UNUSED_WARNING(bar);
#endif
}
回答by Rabbit
You can omit the parameter name in the function prototype, but you must declare it in the function implementation. For example, this compiles and runs just fine under GCC 4.6.1
可以在函数原型中省略参数名,但必须在函数实现中声明。例如,这在 GCC 4.6.1 下编译并运行得很好
void foo(int, int);
void foo(int value, int secondValue)
{
printf("In foo with value %d and %d!\n", value, secondValue);
}
int main(int argc, char **argv)
{
foo(10, 15);
return 0;
}
Outputs: In foo with value 10 and 15!
输出: In foo with value 10 and 15!
As to why (other than because the standards say so): C++ allows you to call a function without using all of the arguments, while C doesn't. If you don't supply all the arguments to a function in C, then the compiler will throw error: too few arguments to function 'foo'
至于为什么(不是因为标准这么说):C++ 允许您在不使用所有参数的情况下调用函数,而 C 则不允许。如果您没有为 C 中的函数提供所有参数,那么编译器将抛出error: too few arguments to function 'foo'