调用 C/++ 时的观察结果和困惑;功能
为什么可以这样调用 C/C++ 函数:
(&myAwesomefunction)(arg1,arg2,arg3...);
获取函数的地址肯定会得到一个指针/地址。据我了解,指针不能作为函数调用。到底发生了什么?
这是否意味着我可以像函数一样调用任何地址?
// Is this legal?
int x = 69;
(&x)(3,78,69.456,'c','o','n','d','o','m');
这意味着什么?
Why can you call C/C++ functions like this:
(&myAwesomefunction)(arg1,arg2,arg3...);
Surely taking the address of a function gives you a pointer/address. And as I understand it pointers are not callable as functions. What's really happening?
Does it mean that I can call any address like a function?
// Is this legal?
int x = 69;
(&x)(3,78,69.456,'c','o','n','d','o','m');
What are the implications of THAT?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相反,指向函数的指针是唯一可以作为函数调用的东西。函数调用运算符的定义如下:
(这意味着您的第二个假设不合法,因为
&x
不具有指向任何类型函数的指针的类型)。那么,您可能会问,为什么还可以调用这样的函数(其中
myAwesomefunction
是声明为函数的标识符)?答案是像
myAwesomefunction
这样的标识符是一个计算结果为函数指示符的主要表达式。函数指示符的定义如下:因此,正式地,当您编写
myAwesomefunction(arg1,arg2,arg3...);
时,myAwesomefunction
计算为函数指示符,然后将其转换为指向该函数的指针,然后用于调用该函数。将函数指示符转换为指向函数的指针类似于将数组类型的表达式转换为指向数组第一个元素的指针。
On the contrary, pointers to functions are the only things callable as functions. The definition of the function call operator starts out:
(This means that your second hypothetical is not legal, because
&x
is does not have a type that is a pointer to a function of any sort).So, you might ask, why can you also call a function like this (where
myAwesomefunction
is an identifier declared as a function)?The answer is that a identifier like
myAwesomefunction
is a primary expression that evaluates to a function designator. The definition of a function designator says:So formally, when you write
myAwesomefunction(arg1,arg2,arg3...);
,myAwesomefunction
evaluates to a function designator, which then is converted to a pointer to the function, which is then used to call the function.This conversion of a function designator to a pointer to the function is similar to the conversion of an expression with array type to a pointer to the array's first element.
是的,第一个是合法的,因为 &myAwesomefunction 将是指向具有已知签名的函数的指针。 C++ 允许您使用与调用函数相同的语法来调用函数指针。
第二个例子是不合法的,因为 C++ 知道 &x 是指向整数的指针,而不是函数。
第一个例子怎么可能呢?编译器知道每个表达式的类型。我知道“myAwesomefunction”是一个函数名称。它知道“&myAwesomefunction”将是一个指向 myAwesomefunction 的指针,并且与 myAwesomefunction 具有相同的签名。然后它会检查 () 并知道给定您有一个函数指针,唯一的逻辑含义是函数调用。它可以确保括号内的内容与函数的签名匹配。
没有歧义,编译器没有理由强迫您在调用函数指针之前取消引用它。如果在函数指针后使用括号,编译器可以找出它的函数调用。
Yes the first is legal because &myAwesomefunction would be a pointer to a function with a known signature. C++ lets you use the same syntax as calling a function in order to call a function pointer.
The second example is NOT legal because C++ knows that &x is pointer to an integer, NOT a function.
How is the first example possible? Well the compiler knows the type of every expression. I knows that "myAwesomefunction" is a function name. It knows that "&myAwesomefunction" would be a pointer which points to myAwesomefunction and has the same signature as myAwesomefunction. It then would examine the ()'s and know that given you have a function pointer, the only logical meaning is a function call. It can ensure that whats inside the paranthesis matches the function's signature.
There's no ambiguity and there no reason for the compiler to force you to dereference a function pointer before you call it. If you use parenthesis after a function pointer the compiler can figure out its a function call.