- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在与某人争论他在this answer的评论线程中提出的建议时,我遇到了一些 gcc4.8 和 VS2013 拒绝编译但 clang 愉快地接受它并显示正确结果的代码。
#include <iostream>
int main()
{
int i{ 5 };
void* v = &i;
std::cout << reinterpret_cast<int&>(*v) << std::endl;
}
Live demo . GCC 和 VC 都因我预期的错误而失败,提示代码试图取消引用 void*
在 reinterpret_cast
内.所以我决定在标准中查找它。来自 N3797,§5.2.10/11 [expr.reinterpret.cast]
A glvalue expression of type
T1
can be cast to the type “reference toT2
” if an expression of type “pointer toT1
” can be explicitly converted to the type “pointer toT2
” using areinterpret_cast
. The result refers to the same object as the source glvalue, but with the specified type. [ Note: That is, for lvalues, a reference castreinterpret_cast<T&>(x)
has the same effect as the conversion*reinterpret_cast<T*>(&x)
with the built-in&
and*
operators (and similarly forreinterpret_cast<T&&>(x)
). —end note ] No temporary is created, no copy is made, and constructors (12.1) or conversion functions (12.3) are not called.
在这种情况下 T1
是void
和 T2
是int
, 和一个 void*
可以转换为int*
使用 reinterpret_cast
.所以所有要求都满足了。
根据注释,reinterpret_cast<int&>(*v)
与 *reinterpret_cast<int*>(&(*v))
效果相同,据我估计,与 *reinterpret_cast<int*>(v)
相同.
那么这是 GCC 和 VC 的错误,还是 clang 而我以某种方式误解了它?
最佳答案
void
类型的表达式被允许作为 return
语句中的主要语法设备,您也可以将表达式转换为 void
,仅此而已:没有 void
类型的泛左值,void
类型的表达式不引用内存。因此,引用的标准中以 glvalue 开头的段落不适用。因此,clang 是错误的。
关于c++ - 取消引用 void 指针时的 reinterpret_cast 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178903/
我最近了解到 C++ 标准包含“严格的别名规则”,它禁止通过不同类型的变量引用相同的内存位置。 但是,该标准确实允许 char 类型合法地别名任何其他类型。这是否意味着 reinterpret_cas
我需要将一段代码从 C++ 转换为 VB6。 具体来说,这个: reinterpret_cast(reinterpret_cast(lParam)->hwndParent) 有人能告诉我这在 VB6
我正在编写与对齐相关的代码,如果给定的指针正确对齐,却没有标准的功能测试,这让我感到非常惊讶。 似乎互联网上的大多数代码都使用(long)ptr或 reinterpret_cast(ptr)测试对齐,
这个问题在这里已经有了答案: Why is reinterpret_cast not constexpr? (2 个答案) 关闭去年。 我遇到了以下错误: class NormalClass { p
我recently learned通过 reinterpret_casting 其地址将 POD 重新解释为不同的 POD 是未定义行为。所以我只是想知道 reinterpret_cast 的潜在用例
考虑以下程序: struct A{}; int main() { A a; A b = a; A c = reinterpret_cast(a); } 编译器 (g++14)
我recently learned通过 reinterpret_casting 其地址将 POD 重新解释为不同的 POD 是未定义行为。所以我只是想知道 reinterpret_cast 的潜在用例
它为什么会存在?在什么情况下它不会导致严格的别名违规,在哪些情况下会导致? 我的意思是,如果你在两个不兼容的类型之间进行转换,并且该转换的结果是唯一指向它在整个程序中使用的内存的指针,那么在假设没有其
这两个cast语句是一样的吗?它们产生相同的结果。 const std::int16_t i = 3; char a[ 2 ]; *reinterpret_cast(a) = i; reinterp
我已经编写了 MyString 和 MyStringConst 类。现在我需要不时地将 MyString 作为 MyStringConst 传递,因此重载强制转换运算符。这是我写的 MyString:
我有一个结构 S,它将两个类型为 T 的固定大小的数组打包在一起。 template struct S { array, 10> x1; array x2; }; 我想获取对大小为 2
如果我已将结构的成员复制到我的类中,我是否可以从我的类转换为结构? #include #include class Buffer { public: void * address;
#include struct A { int a; }; struct I1 : A { int a; }; struct I2 : A { int a; }; struct D : I1, I2
是否可以使用 reinterpret_cast 将 const 限定符仅添加到结构的一个成员。 #include std::pair test; std::pair& ctest = reinter
int main() { class_name object; object.method(); fstream file("writeobject.dat" , ios::o
当我注意到它的输出完全错误时,我正在测试一个简单的编译器。事实上,输出的字节顺序从小变大了。经过仔细检查,违规代码原来是这样的: const char *bp = reinterpret_cast(&
基本思想是创建一个可变大小的数组,在构造时固定,并在单个分配单元中创建另一个类,以减少开销并提高效率。分配缓冲区以适应数组,并使用另一个对象和新放置来构造它们。为了访问数组的元素和另一个对象,使用了指
#include struct I1 { virtual void nb1()=0; virtual ~I1(){} }; struct I2 { virtual void n
我刚刚在使用reinterpret_cast的代码中发现了一个错误,将其更改为dynamic_cast后,问题就消失了。然后,我尝试使用以下代码重现该行为: 1 #include 2 3
我想知道有没有一种不用大量复制就可以解决这个问题的好方法。 例如,假设我有一个字节缓冲区,我在其中保存了很多东西。我会正确地在其中保存 4 个字节的整数和 float 等。 现在,如果我的整数保存在缓
我是一名优秀的程序员,十分优秀!