为什么我可以对static * cast使用void *但不能对char *使用 [英] Why can I use static_cast With void* but not With char*
问题描述
我知道 reinterpret_cast
主要用于去往 char *
。
但是我很惊讶地发现< href = http://en.cppreference.com/w/cpp/language/static_cast rel = nofollow> static_cast
可以做到相同和 void *
。例如:
But I was surprised to find that static_cast
could do the same with a void*
. For example:
auto foo "hello world"s;
auto temp = static_cast<void*>(&foo);
auto bar = static_cast<string*>(temp);
使用 reinterpret_cast
和 char *
超过了 static_cast
和 void *
?
推荐答案
您的问题确实包含两部分:
Your question really has 2 parts:
- 我应该使用
static_cast
还是reinterpret_cast
- 是否应该使用
reinterpret_cast
是指向对象的基础位模式的指针? c $ c> void * 或char *
来解决此基础位模式更可取?
- Should I use
static_cast
orreinterpret_cast
to work with a pointer to the underlying bit pattern of an object without concern for the object type? - If I should use
reinterpret_cast
is avoid*
or achar*
preferable to address this underlying bit pattern?
static_cast
:使用隐式和用户定义的转换组合在类型之间进行转换
static_cast
: Converts between types using a combination of implicit and user-defined conversions
在5.2.9 [expr.static.cast] 13中,该标准实际上给出了示例:
In 5.2.9[expr.static.cast]13 the standard, in fact, gives the example:
T* p1 = new T;
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
它利用隐式强制转换:
指向任何(可选具有cv资格的)对象类型
T
的prvalue指针可以转换为指向(完全具有cv资格的)<$的prvalue指针。 c $ c> void 。结果指针在内存中的位置与原始指针值相同。如果原始指针为空指针值,则结果为目标类型的空指针值。 *
A prvalue pointer to any (optionally cv-qualified) object type
T
can be converted to a prvalue pointer to (identically cv-qualified)void
. The resulting pointer represents the same location in memory as the original pointer value. If the original pointer is a null pointer value, the result is a null pointer value of the destination type.*
但是,类型指针没有隐式转换 T
转换为 char *
。因此,完成该转换的唯一方法是使用 reinterpret_cast
。
There is however no implicit cast from a pointer of type T
to a char*
. So the only way to accomplish that cast is with a reinterpret_cast
.
reinterpret_cast
:在类型之间进行转换通过重新解释基本位模式
reinterpret_cast
: Converts between types by reinterpreting the underlying bit pattern
因此,当您强制转换为a时,回答了问题的第 1 部分 void *
或 char *
您要使用基础位模式 <$ c $应当使用c> reinterpret_cast ,因为它的用途是向读者表示向/从基础位模式的转换。
So in answer to part 1 of your question when you cast to a void*
or a char*
you are looking to work with the underlying bit pattern, reinterpret_cast
should be used because it's use denotes to the reader a conversion to/from the underlying bit pattern.
下一步让我们比较 void *
和 char *
。两者之间的决定可能取决于应用程序。如果要将标准库函数与基础位模式一起使用,请使用该函数接受的类型:
Next let's compare void*
to char*
. The decision between these two may be a bit more application dependent. If you are going to use a standard library function with your underlying bit pattern just use the type that function accepts:
void*
is used in themem
functions provided in thecstring
libraryread
andwrite
usechar*
as inputs
值得注意的是,C ++特定的库更喜欢 char *
指向内存。
保留为内存的 void * 是出于兼容性原因而保留的,因为指针此处。因此,如果您的基础位模式不会使用
cstring
库函数,请使用C ++特定的库行为来回答问题的第 2 部分:更喜欢 char *
而不是 void *
。
It's notable that C++ specific libraries prefer char*
for pointing to memory.
Holding onto memory as a void*
seems to have been preserved for compatibility reasons as pointer out here. So if a cstring
library function won't be used on your underlying bit patern, use the C++ specific libraries behavior to answer part 2 of your question: Prefer char*
to void*
.
这篇关于为什么我可以对static * cast使用void *但不能对char *使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!