假设在我的平台上 sizeof(int)==sizeof(void*),并且我有以下代码:printf( "%p", rand() ); 因为在%p的位置传递了一个非有效指针的值,这会导致未定义行为吗?
在哪些情况下将 char*(或 char[N])进行 reinterpret_cast 是未定义行为,而何时是定义行为?我应该使用什么经验法则来回答这个问题? 正如我们从这个问题中学到的那样,以下是未定义行为:alignas(int) char data[sizeof(int)]; in...
我曾看到在枚举类上使用reinterpret_cast来进行递增操作,我想知道这种用法在标准C++中是否可接受。enum class Foo : int8_t { Bar1, Bar2, Bar3, Bar4, First = Bar1, La...
考虑以下代码片段:static constexpr uint8_t a = 0; static constexpr const int8_t *b = reinterpret_cast<const int8_t *>(&a); 由于C++标准禁止在 constexpr 中使...
我读得越多,就越感到困惑。 与我的问题最相关的最后一个问题是最接近我的问题的,但是所有关于对象生命周期的话语都让我感到困惑,特别是-仅阅读是否可以。 直奔主题,如果我说错了请纠正我。 这很好,gcc没有警告,我正在尝试通过“char *”读取类型为T(uint32_t)的值: ui...
请考虑: float const& f = 5.9e-44f; int const i = (int&) f; 根据expr.cast/4,应该按顺序考虑以下内容: - 一个const_cast - 一个static_cast - 一个static_cast后跟一...
我有一个unsigned char数组,需要转换为std::string,但是我当前的方法使用了reinterpret_cast,我想避免这种方式。 有没有更简洁的方法?unsigned char my_txt[] = { 0x52, 0x5f, 0x73, 0x68, 0x7e, 0x2...
在理论或实践中,将std::pair<T1, T2> const &重新解释为std::pair<T1 const, T2> const &是否安全,假设程序员没有故意进行像特化std::pair<T1 const, T2>这样奇怪的操作?
FFTW手册说明,它的fftw_complex类型与STL中的std::complex<double>类是位兼容的。但这对我来说并不起作用:#include <complex> #include <fftw3.h> int main() { std::...
使用placement new的返回值和其操作数的转换值之间是否存在(语义)差异?struct Foo { ... }; char buffer[...]; Foo *a = new(buffer) Foo; Foo *b = reinterpret_cast<Foo *>(bu...