reinter pret_cast为void *与函数指针工作 [英] reinterpret_cast to void* not working with function pointers

查看:204
本文介绍了reinter pret_cast为void *与函数指针工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要reinter preT投一个函数指针到void *的变量。该类型的函数指针将是类型类*(*)(无效*)

I want to reinterpret cast a function pointer into a void* variable. The type of the function pointer will be of type Class* (*)(void*).

下面是示例code,

class Test
{
    int a;
};

int main()
{
    Test* *p(void **a);
    void *f=reinterpret_cast<void*>(p);   	
}

以上code与Visual Studio / 86的编译器效果很好。但随着ARM编译器,它提供了编译错误。不知道为什么。

The above code works well with Visual Studio/x86 compilers. But with ARM compiler, it gives compilation error. Don't know why.

错误:#694:reinter pret_cast不能
  抛弃const或其他类型的
  预选赛

Error: #694: reinterpret_cast cannot cast away const or other type qualifiers

我读<一个解释href=\"http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type\">http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type

我担心下面的解释。

函数指针之间铸造
  普通指针(例如铸造无效
  (*)(无效)
无效* )。功能
  指针不一定相同
  尺寸为常规指针,因为上
  它们可能包含某些架构
  额外的上下文信息。这个
  可能会工作在x86不错,但
  请记住,这是不确定的行为。

Casting between function pointers and regular pointers (e.g. casting a void (*)(void) to a void*). Function pointers aren't necessarily the same size as regular pointers, since on some architectures they might contain extra contextual information. This will probably work ok on x86, but remember that it's undefined behavior.

如何从做这样的转换无效(*)(无效*) - GT;无效* 有效地使ATLEAST它在大多数编译器编译的几乎是相同的?

How to do such conversions from void (*)(void*) -> void* effectively so that atleast it compiles almost the same in most of the compilers ?

推荐答案

reinter pret_cast 不能用于铸造函数指针到无效* 。虽然有一些额外的东西,一个C投可以做并非由静态组合允许的,reinter preT和const投射,即转换不是其中之一。

reinterpret_cast can't be used to cast a pointer to function to a void*. While there are a few additional things that a C cast can do which aren't allowed by combination of static, reinterpret and const casts, that conversion is not one of them.

在C语言中投是允许的,但它的行为是没有定义(即连往返不保证工作)。

In C the cast is allowed, but it's behavior isn't defined (i.e. even round trip isn't guaranteed to work).

一些POSIX的功能需要转换为有用的好

Some POSIX functions need the conversion to be well useful.

我打过几个编译器我在这里:

I've played with several compilers I've here:


  • 无prevent的C投,即使在最高的一致性模式。一些给取决于预警和一致性级别警告,其他人没有给出任何警告,我试过。

  • 的reinter pret_cast是,即使在较为宽松的水平有些编译一个错误,而其他接受了它在所有的情况下,而没有给予警告。

在函数指针之间进行的C ++ 0x中, reinter pret_cast 最后可用草案和对象指针有条件的支持。

In the last available draft for C++0X, the reinterpret_cast between function pointers and objects pointers is conditionally supported.

请注意,如果该有意义与否将取决于靶比编译器更:便携式编译像gcc会已经行为由目标架构和强加于可能ABI

Note that if that make sense or not will depend on the target more than the compiler: a portable compiler like gcc will have a behavior imposed by the target architecture and possibly ABI.

至于其他有做的话,

Test* *p(void **a);

定义了一个函数,而不是一个指针运行。但功能函数指针隐式转换为以reinter pret_cast的说法做,还等什么reinter pret_cast得到的是一个测试**(* P)(无效**一)

感谢Richard这让我重新审视这个问题更深入(备案,我就在想,该函数指针的指针的对象误认为是所在的C铸允许一个案例中的东西没有用C授权++蒙上组合)

Thanks to Richard which makes me revisit the issue more in depth (for the record, I was mistaken in thinking that the pointer to function to pointer to object was one case where the C cast allowed something not authorized by C++ casts combinations).

这篇关于reinter pret_cast为void *与函数指针工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆