2021年3月5日星期五

Are all non-member function pointers the same size in C++

In the C programming language, we are given the guarantee that a function pointer may legally be converted to a function pointer of a different type and back without loss of data:

Section 6.3.2.3, paragraph 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.

This rule indirectly guarantees that the sizeof one function pointer must be the same as the sizeof any other function pointer, since no loss of data may occur.

However, I am having difficulty finding any similar passages in the C++ standard (currently reading the C++17 standard, if that matters). There are actually few mentions I can find of any conversions to do with function pointers explicitly, but neither conv.ptr nor basic.compound really provide any similar guarantees.

My question is this: Does C++ provide the same guarantee that C does that any (non-member) function pointer may hold the value of any other (non-member) function pointer?


I was hoping to find this already asked, but the closest I could find was this similar question for C (which is not guaranteed to be the same answer as C++), and a bunch of unrelated questions about sizes of member pointers.

To emphasize: This is not asking whether it may work because a compiler supports both C and C++; this is asking whether the C++ abstract machine officially supports this same conversion.

https://stackoverflow.com/questions/66501271/are-all-non-member-function-pointers-the-same-size-in-c March 06, 2021 at 08:39AM

没有评论:

发表评论