据我所知,reinterpret_cast 不得导致数据丢失。
因此不可能在 X86_64 中编译这样的代码,因为整数小于指针
#include <cstdio>
int main() {
int a = 123;
int res = reinterpret_cast<int>(reinterpret_cast<void*>(a));
printf("%d", a == res);
}
Run Code Online (Sandbox Code Playgroud)
问题是:为什么我可以在 GCC 和 Clang 中编译这样的代码?
#include <cstdio>
int main() {
__uint128_t a = 4000000000000000000;
a *= 100;
__uint128_t res = reinterpret_cast<__uint128_t>(reinterpret_cast<void*>(a));
printf("%d", a == res);
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果是“0”,表示有数据丢失。
编辑
我认为有 3 种可能的变体。编译器错误、滥用规范或规范的后果。这是哪个?
我有以下 C/C++ 代码,它使用__builtin_return_address:
#include <stdio.h>
#ifdef __clang__
# define optnone __attribute__((optnone))
#else
# define optnone __attribute__((optimize("O0")))
#endif
void *f() {
return __builtin_extract_return_addr(__builtin_return_address(2));
}
optnone void nest1() {
printf("%p\n", f());
}
optnone void nest2() {
nest1();
}
optnone void nest3() {
nest2();
}
optnone void nest4() {
nest3();
}
optnone int main() {
nest4();
}
Run Code Online (Sandbox Code Playgroud)
GCC生成以下程序集并且工作正常(不会崩溃):
f:
push rbp
mov rbp, rsp
mov rax, QWORD PTR [rbp+0]
pop rbp
mov rax, QWORD PTR [rax]
mov rax, QWORD …Run Code Online (Sandbox Code Playgroud) 我想将一个 freezeset 放入其自身中并将一个元组放入其自身中。
看起来很容易。通过编写一个简单的 C++ 扩展,我能够输出以下内容:
frozenset({frozenset(...)})
((...),)
Run Code Online (Sandbox Code Playgroud)
...意味着该对象在其自身内部
是否可以仅使用 python 及其标准库来执行相同的操作?