我应该使用 C++ reinterpret_cast 优于 C 风格的强制转换?

发布于 2024-12-19 22:28:54 字数 359 浏览 0 评论 0原文

我有以下模板函数,用于将任何标准类型的数据转储到二进制输出流中。

template<typename T> static void
dump ( const T& v, ostream& o ) {
    o.write ( reinterpret_cast<const char*>(&v), sizeof(T));
}

我还可以使用 C 风格(const char*)来代替reinterpret_cast。使用reinterpret_cast 有什么特殊原因吗?我读过一些其他的文章,其中reinterpret_cast 被人皱眉。但上面的用法是合法的,不能用其他东西代替,对吗?

I have the following template function used to dump data of any standard type into a binary output stream.

template<typename T> static void
dump ( const T& v, ostream& o ) {
    o.write ( reinterpret_cast<const char*>(&v), sizeof(T));
}

Instead of the reinterpret_cast I could also use a C-style (const char*). Is there any particular reason to use reinterpret_cast? I read a few other posts where reinterpret_cast was frowned upon. But the above usage is legal and cannot be replaced with anything else, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

菩提树下叶撕阳。 2024-12-26 22:28:54

C-Style 类型转换的问题在于它们在幕后做了很多工作。请参阅此处了解详细说明: http://anteru.net/2007/12/18/200 /

您应该尝试始终使用 C++ 类型转换,从长远来看,这会让生活变得更轻松。在这种情况下,C 风格转换的主要问题是,您可以编写 (char*)(&v) 而使用 reinterpret_cast,则需要额外的 < code>const_cast,所以更安全一些。另外,您可以使用正则表达式轻松找到reinterpret_cast,这对于 C 风格的强制转换是不可能的。

The problem with C-Style casts is that they do a lot under the hood. See here for a detailed explanation: http://anteru.net/2007/12/18/200/

You should try to always use the C++-casts, makes life easier in the long run. The main problem with C-style casts in this case is that you could have written (char*)(&v) while with reinterpret_cast, you would need an additional const_cast, so it's a bit safer. Plus you can easily find reinterpret_cast with a regex, which is not possible for the C-style casts.

冷弦 2024-12-26 22:28:54

没有什么区别。在给定的情况下,C 风格的演员阵容恰恰是“重新解释”演员阵容。

您应该更喜欢 C++ 风格的强制转换的原因是它们对于所强制转换的内容是明确的。如有必要,C 风格的强制转换将始终尝试使用最原始的强制转换,而 C++ 风格的强制转换仅在可能的情况下才进行编译:静态强制转换仅在值可转换或指针/引用时才会成功是兼容的,并且只有当源和目标是彼此的 cv 限定版本时,const 转换才有效。重新解释转换明确表明您希望检查底层二进制表示形式。 (请注意,唯一有效的重新解释转换通常是那些 void 或 char 指针,除非它们是某些更大诡计的一部分。)

There is no difference. In the given situation, the C-style cast is precisely a "reinterpret"-cast.

The reason you should prefer C++-style casts is that they are explicit about what they are casting. A C-style cast will always try to fall back on the crudest possible cast if necessary, while the C++-style cast only compiles if it is possible as intended: a static cast only succeeds if either the values are convertible or the pointers/references are compatible, and a const-cast only works if source and target are cv-qualified versions of one another. A reinterpret-cast states explicitly that you wish to examine an underlying binary representation. (Note that the only valid reinterpret-casts are usually those to void- or char-pointer, unless they're part of some larger trickery.)

一直在等你来 2024-12-26 22:28:54

C风格的铸造是非常非常危险的。因此,C++ 根据典型用法将转换分类为以下类型,

dynamic_cast(表达式) - 允许在适当的类层次结构之间进行转换。

const_cast(表达式) - 抛弃 const 性。

static_cast(表达式) - 在一定程度上是 C 风格,但仍然尊重类型之间的一些不兼容性并且不允许。

reinterpret_cast(表达式) - 如果仍然不满足要求,则可以使用此方法。 C 风格的铸造,但有名字。因此,在大型代码库中很容易找到它。

注意:- 大多数“reinterpret_cast”可以通过适当的设计来消除。换句话说,需要“reinterpret_cast”意味着设计中很可能存在错误。

更新:
这应该是最后一个选项,在上面的情况下,用法是正确的。现在提到reinterpret_cast会给读者这样的印象:作者故意选择不关心类型安全。但使用 c 风格的转换不会给人这种印象。

C style casting is very very dangerous. So C++ categorical divided the casting to below types based on typical usage,

dynamic_cast(expression) - Allows casting between proper class hierarchic.

const_cast(expression) - Casts away const-ness.

static_cast(expression) - To an extent C style but still respects some incompatibilities between types and do not allow.

reinterpret_cast(expression) - If still the requirement is not met, this is available. C style casting but with a name. So it will be easy to find it in large code base.

Note:- Most "reinterpret_cast" can be eliminated with proper design. In other words "reinterpret_cast" is needed means, most-likely something is wrong in the design.

Update:
This should be the last option, and in the case above, the usage is correct. Now mentioning reinterpret_cast will give the reader the impression that intentionally the writer have chosen not to care type safety. But using c style casting will not give that impression.

各空 2024-12-26 22:28:54

当使用 reinterpret_cast 来替换 static_castdynamic_cast 时,会引起不满。鼓励使用它来替代 C 型铸件。

新的演员阵容比 C 风格的演员阵容有优势。一方面,您可以限制实际想要的演员阵容,另一方面,对新演员阵容进行文本搜索比对 C 演员阵容进行文本搜索要容易得多。

reinterpret_cast is frowned upon when it's used to replace a static_cast or dynamic_cast. Using it to replace a C cast is encouraged.

The new casts have benefits over C-style casts. For one, you can limit what cast you actually want, for another it's far easier to do a textual search for the new casts than for C casts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文