简体   繁体   English

(函数指示符)和{函数指针}之间的区别?

[英]Difference between (function designator) and {function pointer}?

In clang, a pointer to a function and the designation/designator have distinct resulting grouping symbols, parenthesis compared to curly braces as seen in the code fragments further down.在 clang 中,指向 function 的指针和指示符/指示符具有不同的结果分组符号,括号花括号相比,如在更下方的代码片段中所见。

What is:什么是:

type * type(*)(type)

used for and versus/what is:用于和对比/什么是:

type *(type)

used for?用于? Curly braces are for use in lists, compound statements and aggregates while parenthesis are primarily used for ordering, grouping, casting and arguments.大括号用于列表、复合语句和聚合,而圆括号主要用于排序、分组、转换和 arguments。

It looks like the first is a type reference to a function pointer accepting a specific type argument which, if enclosed in parenthesis either orders the group be evaluated individually if part of a larger expression or creates a cast which after the resolution of the function would convert it to a specific value of the return type of the specified size and alignment requirements instead of the binary sequence containing the value.看起来第一个是对接受特定类型参数的 function 指针的类型引用,如果包含在括号中,则命令单独评估组(如果是较大表达式的一部分)或创建一个强制转换,在 function 的解析后将转换它以指定大小和 alignment 要求的返回类型的特定值代替包含该值的二进制序列。

The second one is in a block with curly braces and thus a compound object or statement block since I can't see how it would be attached to an aggregate though it may be reference to a function body result of a call.第二个是在一个带有花括号的块中,因此是一个复合 object 或语句块,因为我看不到它如何附加到聚合,尽管它可能引用调用的 function 主体结果。

The second snippet looks like a type reference to a type defined or object group and, since a type may not share a name with an object, effectively making it a keyword, I presume that this somehow creates a named object that is multiplied with itself, or is a reverse cast or something since keywords with an asterisk have no real use aside from object declarations.第二个片段看起来像是对定义的类型或 object 组的类型引用,并且由于类型可能不与 object 共享名称,因此有效地使其成为关键字,我认为这会以某种方式创建一个与自身相乘的命名 object,或者是反向转换或其他东西,因为除了 object 声明外,带星号的关键字没有实际用途。

P

Why does the initial have parenthesis enclosing the symbol whereas the second symbol uses curly braces ?为什么第一个符号用圆括号括起来,而第二个符号使用大括号

For example, the following code fragment when compiled,例如,编译时的以下代码片段,

. .

(*function)(1);

mentions the symbol in the comments after compiling is noted as:编译后注释中提到的符号记为:

(type *(*)(type))

with parenthesis encompassing the representation in the compilation log of the console.括号包含控制台编译日志中的表示。

. .

whereas:然而:

. .

function(1);

mentions the symbol in the comments after compiling is noted as:编译后注释中提到的符号记为:

{type *(type)}

with curly braces encompassing the representation in the compilation log of the console.大括号包含控制台编译日志中的表示。

. .

What is the distinction between the two for, and under what circumstances is it practically applied in any usual/useful contexts?两者之间的区别是什么,在什么情况下它实际上适用于任何通常/有用的上下文?

My general observations of the syntax and semantics of the C language suggest to me the first appears to be a basic ordered-group expression which may be used in a comma-separated list or as a function parameter whereas the second appears to be a struct, union or block statement.我对 C 语言的语法和语义的一般观察表明,第一个似乎是一个基本的有序组表达式,可以在逗号分隔列表中使用或用作 function 参数,而第二个似乎是一个结构,联合或块语句。

I was exploring function pointers and so I tried both and those two notes were clang's comments of the compilation.我正在探索 function 指针,所以我尝试了两个,这两个注释是 clang 对编译的评论。 None of the other 5 posts I read seemed to clarify the nature of the grouping symbols in any context but did provided additional insight to function pointers and their applications.我阅读的其他 5 篇文章似乎都没有阐明任何上下文中分组符号的性质,但确实提供了对 function 指针及其应用的额外见解。

Thank you!谢谢!

function designator is an expression with function type. function 指示符是一个类型为 function 的表达式。

for example in例如在

a = foo(x);

foo is a function designator. foo 是一个 function 指示符。

Function pointer is a reference to the function. Function designators decay to function pointers when used as values. Function pointer是对 function 的引用。Function 指示符在用作值时衰减为 function 指针。

int (*fptr)(int) = foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM