调用 C/++ 时的观察结果和困惑;功能

发布于 2024-12-13 12:36:37 字数 299 浏览 0 评论 0原文

为什么可以这样调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-12-20 12:36:37

相反,指向函数的指针是唯一可以作为函数调用的东西。函数调用运算符的定义如下:

表示被调用函数的表达式应具有类型
指向返回 void 或返回其他对象类型的函数的指针
比数组类型。

(这意味着您的第二个假设合法,因为&x不具有指向任何类型函数的指针的类型)。

那么,您可能会问,为什么还可以调用这样的函数(其中 myAwesomefunction 是声明为函数的标识符)?

myAwesomefunction(arg1,arg2,arg3...);

答案是像 myAwesomefunction 这样的标识符是一个计算结果为函数指示符的主要表达式。函数指示符的定义如下:

函数指示符是具有函数类型的表达式。除了
当它是 sizeof 运算符或一元 & 的操作数时
运算符,类型为“函数返回类型”的函数指示符
转换为类型为“函数指针”的表达式
返回类型''。

因此,正式地,当您编写 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:

The expression that denotes the called function shall have type
pointer to function returning void or returning an object type other
than an array type.

(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)?

myAwesomefunction(arg1,arg2,arg3...);

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:

A function designator is an expression that has function type. Except
when it is the operand of the sizeof operator or the unary &
operator, a function designator with type ‘‘function returning type’’
is converted to an expression that has type ‘‘pointer to function
returning type’’.

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.

葬花如无物 2024-12-20 12:36:37

是的,第一个是合法的,因为 &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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文