为什么我们在C ++中有两个链接的static_cast可以做它的工作,reinterpret_cast? [英] Why do we have reinterpret_cast in C++ when two chained static_cast can do its job?
问题描述
假设我想将 A *
转换为 char *
,反之亦然,我们有两个选择我们很多人认为我们有两种选择:因为看起来都有效!)
struct A
{
int age;
char name [128];
};
A a;
char * buffer = static_cast< char *>(static_cast< void *>(& a)); // choice 1
char * buffer = reinterpret_cast< char *>(& a); // choice 2
两个都很正常。
// convert back
A * pA = static_cast< A *>(static_cast< void *>(buffer)); // choice 1
A * pA = reinterpret_cast< A *>(buffer); // choice 2
即使这样工作正常!
那么,为什么两个链接 static_cast
可以做的时候在C ++中有 reinterpret_cast
它的工作?
有些人可能会认为这个主题是与之前的主题重复,如列在这篇文章的底部,但它不是。这些主题只在理论上讨论,但没有一个给出甚至一个示例为什么 reintepret_cast
是真正需要的,两个 static_cast
会肯定失败。我同意,一个static_cast将失败。但是如何两个呢?
如果两个链接 static_cast
的语法看起来很麻烦,以使它更加程序员友好:
模板< class To,class From>
到any_cast(From v)
{
return static_cast< To>(static_cast< void *>(v));
}
然后我们可以使用:
char * buffer = any_cast< char *>(& a); // choice 1
char * buffer = reinterpret_cast< char *>(& a); //选择2
//转回
A * pA = any_cast< A *>(buffer); // choice 1
A * pA = reinterpret_cast< A *>(buffer); // choice 2
另请参阅 any_cast
可能有用:正确投射fstream读取并写成员函数。
所以我的问题基本上是,
- 为什么我们在C ++中有
reinterpret_cast
? - 请告诉我一个例子, >
static_cast
肯定不能做同样的工作?
static_cast
s可以做的(全部从C ++ 03 5.2.10): -
指针可以显式转换为任何积分
-
整数类型或枚举类型的值可以显式转换为指针。
-
指向函数的指针可以显式转换为指向不同类型函数的指针。
-
类型
T1
的X
成员的类型指针可以显式转换为类型指向成员的指针的右值T1
和类型
T2
的Y
此外,从C ++ 03 9.2 / 17:
- 指向POD结构体对象的指针,使用
reinterpret_cast
,指向其初始成员(或者如果该成员是位字段,则指向它所在的单元),反之亦然。
Say I want to cast A*
to char*
and vice-versa, we have two choices (I mean, many of us think we've two choices, because both seems to work! Hence the confusion!):
struct A
{
int age;
char name[128];
};
A a;
char *buffer = static_cast<char*>(static_cast<void*>(&a)); //choice 1
char *buffer = reinterpret_cast<char*>(&a); //choice 2
Both work fine.
//convert back
A *pA = static_cast<A*>(static_cast<void*>(buffer)); //choice 1
A *pA = reinterpret_cast<A*>(buffer); //choice 2
Even this works fine!
So why do we have reinterpret_cast
in C++ when two chained static_cast
can do its job?
Some of you might think this topic is a duplicate of the previous topics such as listed at the bottom of this post, but it's not. Those topics discuss only theoretically, but none of them gives even a single example demonstrating why reintepret_cast
is really needed, and two static_cast
would surely fail. I agree, one static_cast would fail. But how about two?
If the syntax of two chained static_cast
looks cumbersome, then we can write a function template to make it more programmer-friendly:
template<class To, class From>
To any_cast(From v)
{
return static_cast<To>(static_cast<void*>(v));
}
And then we can use this, as:
char *buffer = any_cast<char*>(&a); //choice 1
char *buffer = reinterpret_cast<char*>(&a); //choice 2
//convert back
A *pA = any_cast<A*>(buffer); //choice 1
A *pA = reinterpret_cast<A*>(buffer); //choice 2
Also, see this situation where any_cast
can be useful: Proper casting for fstream read and write member functions.
So my question basically is,
- Why do we have
reinterpret_cast
in C++? - Please show me even a single example where two chained
static_cast
would surely fail to do the same job?
- Which cast to use; static_cast or reinterpret_cast?
- Cast from Void* to TYPE* : static_cast or reinterpret_cast
There are things that reinterpret_cast
can do that no sequence of static_cast
s can do (all from C++03 5.2.10):
A pointer can be explicitly converted to any integral type large enough to hold it.
A value of integral type or enumeration type can be explicitly converted to a pointer.
A pointer to a function can be explicitly converted to a pointer to a function of a different type.
An rvalue of type "pointer to member of
X
of typeT1
" can be explicitly converted to an rvalue of type "pointer to member ofY
of typeT2
" ifT1
andT2
are both function types or both object types.
Also, from C++03 9.2/17:
- A pointer to a POD-struct object, suitably converted using a
reinterpret_cast
, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.
这篇关于为什么我们在C ++中有两个链接的static_cast可以做它的工作,reinterpret_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!