简体   繁体   English

为什么不是指向成员函数的指针只是像数据指针那样的内存地址

[英]Why aren't pointers to member functions just memory address like data pointers

I realized from this FAQ entry that one cannot convert a pointer to member function to/from void* . 我从这个 FAQ条目中意识到,无法将指向成员函数的指针转换为void* The reason being pointers to members are not memory addresses exactly like pointers to data! 指向成员的原因不是内存地址,就像数据指针一样! Why so? 为什么这样? Please help me get clarified. 请帮我澄清一下。 And this isn't necessarily with member functions but any normal C functions as well, isn't? 这不一定与成员函数,但任何正常的C函数,不是吗?

pointers to members are not memory addresses exactly like pointers to data! 指向成员的指针不是内存地址,就像数据指针一样! Why so? 为什么这样?

Pointers to member functions need to indicate whether the function is virtual, and allow virtual dispatch (perhaps by specifying the index into the vtable, rather than the address of a specific function) if so. 指向成员函数的指针需要指示函数是否为虚函数,并允许虚拟调度(可能通过将指数指定到vtable中,而不是指定特定函数的地址),如果是这样的话。 This makes them more complicated than just an address. 这使得它们比仅仅地址更复杂。

And this isn't necessarily with member functions but any normal C functions as well, isn't? 这不一定与成员函数,但任何正常的C函数,不是吗?

Pointers to "normal" (non-member) functions may be converted to object pointers, but not portably. 指向“普通”(非成员)函数的指针可以转换为对象指针,但不能移植。 Quoting the standard: 引用标准:

C++11 5.2.10/8 Converting a function pointer to an object pointer type or vice versa is conditionally-supported. C ++ 11 5.2.10 / 8有条件地支持将函数指针转换为对象指针类型,反之亦然。 The meaning of such a conversion is implementation-defined, [...] 这种转换的意义是实现定义的,[...]

On many platforms, a (non-member) function pointer is simply a memory address, and the conversion is well-defined. 在许多平台上,(非成员)函数指针只是一个内存地址,转换是明确定义的。 Some platforms have more exotic memory architectures - for example, separate memory spaces for instructions and data - and the conversion may not be allowed on those platforms. 某些平台具有更多奇特的内存架构 - 例如,用于指令和数据的单独内存空间 - 并且可能不允许在这些平台上进行转换。

In the C++ standard 在C ++标准中

As the next FAQ says: 正如下一个FAQ所说:

The language does not require functions and data to be in the same address space, so, by way of example and not limitation, on architectures that have them in different address spaces, the two different pointer types will not be comparable. 该语言不要求函数和数据在同一地址空间中,因此,作为示例而非限制,对于将它们置于不同地址空间中的体系结构,两种不同的指针类型将不具有可比性。

In §5.2.10/8 (of N3936 specifically) the standard specifies that this is indeed implementation defined : 在§5.2.10/ 8(具体为N3936)中,该标准规定这确实是实现定义的

Converting a function pointer to an object pointer type or vice versa is conditionally-supported. 有条件地支持将函数指针转换为对象指针类型(反之亦然)。 The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cv- qualification, shall yield the original pointer value. 这种转换的含义是实现定义的,除非实现支持两个方向的转换,将一种类型的prvalue转换为另一种类型并返回,可能具有不同的cv-限定,将产生原始指针值。

Here the behavior is well specified. 这里的行为很明确。

In the C standard 在C标准中

The C standard doesn't appear to contemplate the conversion from a function pointer to an object pointer. C标准似乎没有考虑从函数指针到对象指针的转换。 In fact it barely draws a line between them. 事实上,它们之间几乎没有界线。

It just states, at §6.3.2.3/8, that: 它只是在§6.3.2.3/ 8中指出:

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; 指向一种类型的函数的指针可以被转换为指向另一种类型的函数的指针并且再次返回; the result shall compare equal to the original pointer. 结果应该等于原始指针。 If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined. 如果转换的指针用于调用类型与引用类型不兼容的函数,则行为未定义。

At this point the behavior almost seems to be unspecified . 在这一点上,行为几乎似乎没有具体说明

And then later in §6.5.9/6: 然后在§6.5.9/ 6中:

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space. 两个指针比较相等,当且仅当两个都是空指针时,两者都是指向同一对象的指针(包括指向对象的指针和开头的子对象)或函数,两者都是指向同一数组的最后一个元素的指针对象,或者一个指向一个数组对象末尾的指针,另一个是指向紧跟在地址空间中第一个数组对象之后的另一个数组对象的开头的指针。

Here we can see the only trace of an actual difference in: 在这里,我们可以看到实际差异的唯一痕迹:

Two pointers compare equal if and only if both are [..] pointers to the same object (..) or function [..]. 当且仅当两个指针都是指向同一对象(..) 函数[...]的[..]指针时,两个指针才相等。

The why 为什么呢

As for the "why", it appears to be dependent on the fact that some architectures simply have functions and objects in two address space. 至于“为什么”,它似乎取决于一些架构只在两个地址空间中具有功能和对象的事实。

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

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