C++ 什么是形式参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18870156/
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
What is a formal parameter?
提问by CygnusX1
When compiling in C++ I often end up with error messages dealing with "formal parameters", such as
在 C++ 中编译时,我经常以处理“形式参数”的错误消息告终,例如
error C2719: 'b': formal parameter with __declspec(align('16')) won't be aligned
I do understand the error, and the fact that b
is a parameter of a function I am defining.
我确实理解错误,以及这b
是我定义的函数的参数这一事实。
However, what does it mean that a parameter is formal? Can there be informalparameters as well?
但是,参数是正式的是什么意思?也可以有非正式参数吗?
I do notice that the term "formal parameter" appears in other languages as well, so I presume it is a more generic term not necessarily specific to C-family of languages? Are informal parameters supported by some subset of languages?
我确实注意到术语“形式参数”也出现在其他语言中,所以我认为它是一个更通用的术语,不一定特定于 C 系列语言?某些语言子集是否支持非正式参数?
Upon seeing the answers, one final question: Where those names formal parameterand actual parameterorigin from? Does it origin from the C standard, or is it an effect of calling it as such in some abstract language calculus?
看到答案,最后一个问题:形参和实参的名字从何而来?它是源自 C 标准,还是在某些抽象语言演算中这样称呼它的结果?
回答by cpp
There are formaland actualparameters:
有正式的和实际参数:
void foo(int arg); //arg is a formal parameter
int main()
{
int val = 1;
foo(val); //val is an actual parameter
}
From C++ Standard:
从C++ 标准:
1.3.1 formal parameter (parameter)
1.3.1 形参(parameter)
an object or reference declared as part of a function declaration or definition, or in the catch clause of an exception handler, that acquires a value on entry to the function or handler; an identifier from the comma-separated list bounded by the parentheses immediately following the macro name in a function-like macro definition; or a template-parameter. Parameters are also known as formal arguments or formal parameters.
作为函数声明或定义的一部分,或在异常处理程序的 catch 子句中声明的对象或引用,在进入函数或处理程序时获取值;来自逗号分隔列表的标识符,由紧跟在类似函数的宏定义中的宏名称后面的括号括起来;或模板参数。参数也称为形式参数或形式参数。
1.3.10 actual parameter (argument)
1.3.10 实参(argument)
an expression in the comma-separated list bounded by the parentheses in a function call expression, a sequence of preprocessing tokens in the comma-separated list bounded by the parentheses in a function-like macro invocation, the operand of throw, or an expression, type-id or template-name in the comma-separated list bounded by the angle brackets in a template instantiation. Also known as an actual argument or actual parameter.
以函数调用表达式中的括号为界的逗号分隔列表中的表达式、在类似函数的宏调用中以括号为界的逗号分隔列表中的预处理标记序列、throw 的操作数或表达式, type-id 或 template-name 在以模板实例化中的尖括号为界的逗号分隔列表中。也称为实参或实参。
回答by SingerOfTheFall
Formal parameters are the parameters known at the function definition. The actual parameters are what you actually(hence the name) pass to the function when you call it.
形式参数是在函数定义中已知的参数。实际参数是您在调用函数时实际(因此得名)传递给函数的参数。
void foo( int a ); // a is a formal parameter
foo(10); // 10 is the actual parameter
回答by Kerrek SB
It's a matter of being a little pedantic over terminology, but quite useful: The formal parameters are what you just think of function parameters:
这是一个对术语有点迂腐的问题,但非常有用:形式参数就是你刚刚想到的函数参数:
int foo(bool a, float b);
Here a
and b
are formal parameters. The point is that in the function body, you're referring to those parameters "formally" without actually knowing their value. It is only when you actual evaluate a function call expressionthat the formal function parameters are boundto the function call arguments:
这里a
和b
是形式参数。关键是在函数体中,您“正式”引用了这些参数,而实际上并不知道它们的值。只有当您实际评估函数调用表达式时,形式函数参数才会绑定到函数调用参数:
int result = foo(false, 1.5);
In this call expression, the value false
of the first argument is bound to the formal parameter a
, and similarly for the second argument.
在这个调用表达式中,false
第一个参数的值绑定到形参a
,第二个参数也类似。
The distinction between parameters and arguments is maybe more important to language designers and comiler writers, but as an example in C++, it can be very helpful to get your head around this when you're trying to follow the rules for template argument deduction.
参数和参数之间的区别对于语言设计者和编译器编写者来说可能更重要,但作为 C++ 中的一个例子,当您试图遵循模板参数推导规则时,了解这一点会非常有帮助。