当参数名称等于函数名称时,谁胜出?

huangapple go评论44阅读模式
英文:

When parameter name equals function name: who wins?

问题

在C语言中,假设我有一个名为 thing() 的函数以及另一个函数,它将 thing 作为形式参数使用:

thing_t *thing(int id) { ... 返回一个 *thing_t 类型的函数 ... }

void foo(thing_t *thing) { ...  *thing_t 类型参数为参数的函数 ... }

foo() 的函数体内,是否有保证 thing 引用传入的参数,而不是同名的函数?C语言的任何规范是否对此有任何说明?

(是的,我同意这是可疑的编码风格...)

Note: The code portion has been excluded from translation as per your request.

英文:

(I have this uneasy feeling that I've asked this before, but now I can't find it. Feel free to close and redirect this question if so...)

In C, assume I have a function named thing() and some other function that uses thing as a formal parameter:

thing_t *thing(int id) { ... function that returns a *thing_t ... }

void foo(thing_t *thing) { ... function that takes a *thing_t as an argument ... }

Inside the body of foo(), is there any guarantee that thing refers to the passed-in parameter as opposed to the function of the same name? Do any of the C specifications have anything to say about this?

(And yes, I'd agree that this is dubious coding style...)

答案1

得分: 4

thing 函数的定义将 thing 声明为一个具有文件作用域的标识符,根据 C 2018 6.2.1 4。它的作用域从其声明开始(具体来说,从其声明符的末尾,即 *thing(int id))延伸到翻译单元的末尾。

foo 的定义中,参数 thing 的定义将 thing 声明为一个具有块作用域的标识符,同样是根据 6.2.1 4。它的作用域从其声明开始延伸到函数定义体的块的末尾。

然后,6.2.1 4 的最后一句告诉我们,在 foo 内部,thing 指的是参数,而不是函数:

如果一个标识符在同一名称空间中指代两个不同的实体,它们的作用域可能会重叠。如果是这样,一个实体(内部作用域)的作用域将严格在另一个实体(外部作用域)的作用域之前结束。在内部作用域中,标识符指代在内部作用域中声明的实体;在内部作用域中声明的外部作用域中的实体是隐藏的(在内部作用域内不可见)。

英文:

The definition of the function thing declares thing as an identifier with file scope, per C 2018 6.2.1 4. Its scope extends from its declaration (specifically, from the end of its declarator, *thing(int id)) to the end of the translation unit.

The definition of the parameter thing in the definition of foo declares thing as an identifier with block scope, also per 6.2.1 4. Its scope extends from its declaration to the end of the block that is the body of the function definition.

Then the final sentences of 6.2.1 4 tells us that within foo, thing refers to the parameter, not the function:

> If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

答案2

得分: 3

函数的参数位于比文件范围对象(如函数名称)更“内部”的范围,因此参数 thing 遮蔽了名为 thing 的函数。

这在C标准第6.2.1p4节中有详细说明,涉及标识符的作用域:

如果标识符在同一名称空间中指代两个不同的实体,它们的作用域可能会重叠。如果是这样,一个实体(内部作用域)的作用域将严格在另一个实体(外部作用域)的作用域之前结束。在内部作用域内,该标识符指代在内部作用域中声明的实体;在内部作用域中,外部作用域中声明的实体被隐藏(不可见)。

英文:

Parameters to functions are at a more "inner" scope than file scope objects such as function names, so the parameter thing masks the function named thing.

This is spelled out in section 6.2.1p4 of the C standard regarding the scope of identifiers:

> If an identifier designates two different entities in the same name
> space, the scopes might overlap. If so, the scope of one entity (the
> inner scope) will end strictly before the scope of the other entity
> (the outer scope). Within the inner scope, the identifier designates
> the entity declared in the inner scope; the entity declared in the
> outer scope is hidden (and not visible) within the inner scope.

huangapple
  • 本文由 发表于 2023年5月17日 21:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定