为何以及何时被强制转换为char挥发性放大器;需要的? [英] Why and when is cast to char volatile& needed?

查看:117
本文介绍了为何以及何时被强制转换为char挥发性放大器;需要的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href=\"https://svn.boost.org/trac/boost/browser/trunk/boost/utility/addressof.hpp#L37\">boost::detail::addressof_impl::f()一系列 reinter pret_cast 第做是为了获得对象的实际地址的情况下, T类有重载运营商及()

In boost::detail::addressof_impl::f() a series of reinterpret_casts is done to obtain the actual address of the object in case class T has overloaded 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的字符和放大器的目的; ,而不是仅仅铸造字符和放大器;

推荐答案

一个投直字符和放大器; 会失败,如果 T 常量挥发性预选赛 - reinter pret_cast 无法删除这些(但可以将它们添加)和的const_cast 不能使任意类型的变化。

A cast straight to char& would fail if T has const or volatile qualifiers - reinterpret_cast can't remove these (but can add them), and const_cast can't make arbitrary type changes.

这篇关于为何以及何时被强制转换为char挥发性放大器;需要的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆