gpt4 book ai didi

c++ - void(*)() 模拟成员函数指针往返转换

转载 作者:太空狗 更新时间:2023-10-29 21:44:58 25 4
gpt4 key购买 nike

当想要存储指向具有不同签名的函数的指针时,可以将它们转换为 void(*)()(或任何其他函数指针类型),然后在调用之前将它们转换回原始类型而 afaik C++ 保证了这个往返工作。但是成员函数指针呢?是否可以保证通过另一个不同类型的成员函数指针(可能指向不同类的成员函数)来回传递一个成员函数指针不会破坏某些东西?

最佳答案

是的,指针到成员类型之间的reinterpret_cast 往返保证是有效的,只要你保持指针到数据成员或指针到成员-在这两种情况下都起作用。

引用 C++11:

[expr.reinterpret.cast]

10 A prvalue of type "pointer to member of X of type T1" can be explicitly converted to a prvalue of a different type "pointer to member of Y of type T2" if T1 and T2 are both function types or both object types. The null member pointer value (4.11) is converted to the null member pointer value of the destination type. The result of this conversion is unspecified, except in the following cases:

  • converting a prvalue of type "pointer to member function" to a different pointer to member function type and back to its original type yields the original pointer to member value.

关于c++ - void(*)() 模拟成员函数指针往返转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18791527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com