reinterpret_cast to void *不能使用函数指针 [英] reinterpret_cast to void* not working with function pointers

查看:660
本文介绍了reinterpret_cast to void *不能使用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新解释将一个函数指针转换为一个void *变量。函数指针的类型将为 Class *(*)(void *)



示例代码

  class Test 
{
int a;
};

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

上述代码适用于Visual Studio / x86编译器。但是使用ARM编译器,它会产生编译错误。不知道为什么。


错误:#694:reinterpret_cast不能
丢弃const或其他类型
限定符


我阅读了将函数指针转换为另一个类型



我对下面的解释感到担心。


在函数指针和
之间转换常规指针(例如,转换 void
(*)(void)
a void * )。函数
指针不一定与常规指针一样的
大小,因为在
一些架构,他们可能包含
额外的上下文信息。这个
可能在x86上正常工作,但
记住它是未定义的行为。


如何做 void(*)(void *) - >

解决方案

code> reinterpret_cast 不能用于将函数的指针转换为 void * 。虽然还有一些额外的事情,C转换可以做,不允许静态,重新解释和const转换的组合,这种转换不是其中之一。



在C中,允许转换,但它的行为没有定义(即,即使往返不能保证工作)。



有些POSIX函数需要转换才能使用。



ve这里:




  • 无法防止C强制转换,即使在最高一致性模式。有些人根据警告和一致性水平发出警告,其他人没有警告我试过。

  • 即使在更轻松的级别中,reinterpret_cast也是一个错误,而其他编译器在所有情况下都接受它,而不发出警告。



在最后一个可用的C ++ 0X草案中,有条件地支持函数指针和对象指针之间的 reinterpret_cast



请注意,如果这样有意义或者没有意义取决于目标,而不是编译器:像gcc这样的可移植编译器将具有由目标架构和可能的ABI强加的行为。 p>

如其他人所说,

  **一个); 

定义函数,而不是函数指针。但函数指针的函数隐式转换是为reinterpret_cast的参数,所以reinterpret_cast get是一个 Test **(* p)(void ** a)



感谢Richard,让我更深入地回顾这个问题(为了记录,我错误地认为函数的指针指向对象是一种情况C转换允许未经C ++转换组合授权的东西)。


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

Below is the sample code,

class Test
{
    int a;
};

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

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

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

I read the explanation in Casting a function pointer to another type

I was concerned about the below explanation.

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.

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

解决方案

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.

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

Some POSIX functions need the conversion to be well useful.

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

  • none prevent the C cast, even in the highest conformance mode. Some give a warning depending on the warning and conformance level, others gave no warning whatever I tried.
  • the reinterpret_cast was a error with some compilers even in the more relaxed level while other accepted it in all case without ever giving a warning.

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

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.

As other have make the remark,

Test* *p(void **a);

defines a function, not a pointer to function. But the function to pointer to function implicit conversion is made for the argument to reinterpret_cast, so what reinterpret_cast get is a Test** (*p)(void** a).

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).

这篇关于reinterpret_cast to void *不能使用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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