14得票2回答
static_cast 安全性

据我所知,对于指针/引用的static_cast,如果编译器此时无法看到类的定义,则static_cast将像reinterpret_cast一样行事。 static_cast为什么对于数值类型是安全的,而对于指针/引用不安全呢?

13得票3回答
reinterpret_cast在枚举类型中出错

为什么我不能使用 reinterpret_cast 操作符进行这样的转换?enum Foo { bar, baz }; void foo(Foo) { } int main() { // foo(0); // error: invalid conversion from 'int' ...

12得票7回答
你能将一个双精度数组重新解释为包含双精度的结构体吗?

可以将一个双重数组转换为由双精度数构成的结构体吗? struct A { double x; double y; double z; }; int main (int argc , char ** argv) { double arr[3] = {1.0,2.0,3...

12得票3回答
为什么这个static_cast不被允许?

我有一个A类对象,我想将其分配在自定义堆栈对象上。为了做到这一点,我只需将堆栈指针移动与对象大小相同的字节数并返回其先前的值: class A : public B {}; //B is from a precompiled library class stack { public: ...

12得票4回答
为什么我需要使用reinterpret_cast将Fred ** const转换为void ** const?

我有一个指向 Fred 指针的常量指针,但我不明白为什么 static_cast 不足以满足要求。typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const p...

11得票3回答
如何在遵循MISRA C++标准的情况下实现CRTP

我的团队正在开发一个嵌入式系统,我们需要遵循MISRA C++。 我们正在重构代码,以使用更少的虚拟方法,因此我们正在尝试实现CRTP,以使用静态多态而不是动态多态。 但我们遇到了问题,静态多态需要指针转换,因此我们的静态分析检查器会报错。 这是接口: template <typ...

11得票1回答
static_cast和reinterpret_cast用于std::aligned_storage吗?

请问有人能解释一下关于类型转换的代码吗?具体在http://en.cppreference.com/w/cpp/types/aligned_storage中。 以下代码可以吗: return *static_cast<const T*>(static_cast<const...

11得票1回答
为什么在gcc的is_nothrow_constructible实现中需要static_cast?

从 GCC 实现的 type_traits 中获取,为什么这里需要使用 static_cast?template <typename _Tp, typename... _Args> struct __is_nt_constructible_impl : public int...

10得票4回答
奇怪的static_cast技巧?

在查看Qt源代码时,我发现了这个宝石: template <class T> inline T qgraphicsitem_cast(const QGraphicsItem *item) { return int(static_cast<T>(0)->T...

10得票1回答
如何在使用虚继承时使用 static_cast?

因此,在虚拟继承中使用static_cast进行向下转换是不可能的,但是如何进行以下向上转换: class Base {...}; class Derived : public virtual Base {...}; ... Derived *d = new Derived(); Bas...