是reinterpret_cast和c风格的cast兼容(通过C ++标准)? [英] Is reinterpret_cast and c-style cast compatible (by C++ standard)?

查看:345
本文介绍了是reinterpret_cast和c风格的cast兼容(通过C ++标准)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准提到 reinterpret_cast 是实现定义的,除了强制转换(使用 reinterpret_cast )到原始类型将导致原始值传递给第一个。

The C++ standards mentions that reinterpret_cast is implementation defined, and doesn't give any guarantees except that casting back (using reinterpret_cast) to original type will result in original value passed to first.

C风格的铸造至少某些类型的行为方式大致相同 - 使用相同的值来回传递结果 - 目前我正在使用枚举和 int s,但也有一些其他的例子。

C-style casting of at least some types behaves much the same way - casting back and forth results with the same value - Currently I am working with enumerations and ints, but there are some other examples as well.

虽然C ++标准给这两个类型的定义,它也给混合铸件同样的保证?如果库X从函数 int Y()返回一些枚举值,可以使用上面的任何转型, cast用于在Y的身体中将初始枚举转换为 int ?我没有X的源代码,所以我不能检查(它可以改变与下一个版本反正),这样的东西在文档中几乎没有提到。

While C++ standard gives those definitions for both cast-styles, does it also give the same guarantee for mixed casts? If library X returns from function int Y() some enum value, can use any of above casts, without worrying what cast was used to convert initial enum to int in Y's body? I don't have X's source code, so I cannot check (and it can change with next version anyway), and things like that are hardly mentioned in documentation.

I知道在大多数实现中,在这种情况下,两个cast的行为是一样的;我的问题是:C ++标准对这样的情况 - 如果有的话。

I know that under most implementations in such cases both casts behave the same; my question is: what does C++ standard say about such cases - if anything at all.

推荐答案

C ++定义C语言以 static_cast const_cast reinterpret_cast 的形式转换语法。所以你得到相同的保证相同的操作任何语法你用来实现它。

C++ defines the semantic of the C cast syntax in terms of static_cast, const_cast and reinterpret_cast. So you get the same guaranteed for the same operation whatever syntax you use to achieve it.

这篇关于是reinterpret_cast和c风格的cast兼容(通过C ++标准)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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