据我所知,对于指针/引用的static_cast,如果编译器此时无法看到类的定义,则static_cast将像reinterpret_cast一样行事。 static_cast为什么对于数值类型是安全的,而对于指针/引用不安全呢?
为什么我不能使用 reinterpret_cast 操作符进行这样的转换?enum Foo { bar, baz }; void foo(Foo) { } int main() { // foo(0); // error: invalid conversion from 'int' ...
可以将一个双重数组转换为由双精度数构成的结构体吗? struct A { double x; double y; double z; }; int main (int argc , char ** argv) { double arr[3] = {1.0,2.0,3...
我有一个A类对象,我想将其分配在自定义堆栈对象上。为了做到这一点,我只需将堆栈指针移动与对象大小相同的字节数并返回其先前的值: class A : public B {}; //B is from a precompiled library class stack { public: ...
我有一个指向 Fred 指针的常量指针,但我不明白为什么 static_cast 不足以满足要求。typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const p...
我的团队正在开发一个嵌入式系统,我们需要遵循MISRA C++。 我们正在重构代码,以使用更少的虚拟方法,因此我们正在尝试实现CRTP,以使用静态多态而不是动态多态。 但我们遇到了问题,静态多态需要指针转换,因此我们的静态分析检查器会报错。 这是接口: template <typ...
请问有人能解释一下关于类型转换的代码吗?具体在http://en.cppreference.com/w/cpp/types/aligned_storage中。 以下代码可以吗: return *static_cast<const T*>(static_cast<const...
从 GCC 实现的 type_traits 中获取,为什么这里需要使用 static_cast?template <typename _Tp, typename... _Args> struct __is_nt_constructible_impl : public int...
在查看Qt源代码时,我发现了这个宝石: template <class T> inline T qgraphicsitem_cast(const QGraphicsItem *item) { return int(static_cast<T>(0)->T...
因此,在虚拟继承中使用static_cast进行向下转换是不可能的,但是如何进行以下向上转换: class Base {...}; class Derived : public virtual Base {...}; ... Derived *d = new Derived(); Bas...