代码之家  ›  专栏  ›  技术社区  ›  sharptooth

为什么和什么时候被铸造成易挥发的和需要的?

  •  11
  • sharptooth  · 技术社区  · 15 年前

    boost::detail::addressof_impl::f() 一系列 reinterpret_cast s用于获取对象的实际地址,以防 class T 已超载 operator&() :

    template<class T> struct addressof_impl
    {
        static inline T* f( T& v, long )
        {
            return reinterpret_cast<T*>(
                &const_cast<char&>(reinterpret_cast<const volatile char&>(v)));
        }
    }
    

    演员表的目的是什么 const volatile char& 而不是仅仅铸造到 char& ?

    2 回复  |  直到 15 年前
        1
  •  9
  •   Mike Seymour    15 年前

    直投 char& 如果失败了 T const volatile 限定词- reinterpret_cast 无法删除这些(但可以添加它们),并且 const_cast 无法进行任意类型更改。

        2
  •  4
  •   Ari    15 年前

    对象可能是 const volatile 或者两者兼而有之(这可能是矛盾的),在这种情况下,这样做可能是违法的。 reinterpret_cast 它的类型缺少这些属性。(往相反的方向走当然不是问题)。

    推荐文章