<tfoot id='T7rq7'></tfoot>

<small id='T7rq7'></small><noframes id='T7rq7'>

<legend id='T7rq7'><style id='T7rq7'><dir id='T7rq7'><q id='T7rq7'></q></dir></style></legend>

    1. <i id='T7rq7'><tr id='T7rq7'><dt id='T7rq7'><q id='T7rq7'><span id='T7rq7'><b id='T7rq7'><form id='T7rq7'><ins id='T7rq7'></ins><ul id='T7rq7'></ul><sub id='T7rq7'></sub></form><legend id='T7rq7'></legend><bdo id='T7rq7'><pre id='T7rq7'><center id='T7rq7'></center></pre></bdo></b><th id='T7rq7'></th></span></q></dt></tr></i><div id='T7rq7'><tfoot id='T7rq7'></tfoot><dl id='T7rq7'><fieldset id='T7rq7'></fieldset></dl></div>
        <bdo id='T7rq7'></bdo><ul id='T7rq7'></ul>
      1. 在 C++ 中获取变量地址的最便携和可靠的方法

        时间:2023-10-06

            <small id='VDYIx'></small><noframes id='VDYIx'>

            <legend id='VDYIx'><style id='VDYIx'><dir id='VDYIx'><q id='VDYIx'></q></dir></style></legend>

                <tbody id='VDYIx'></tbody>
                • <bdo id='VDYIx'></bdo><ul id='VDYIx'></ul>
                  <i id='VDYIx'><tr id='VDYIx'><dt id='VDYIx'><q id='VDYIx'><span id='VDYIx'><b id='VDYIx'><form id='VDYIx'><ins id='VDYIx'></ins><ul id='VDYIx'></ul><sub id='VDYIx'></sub></form><legend id='VDYIx'></legend><bdo id='VDYIx'><pre id='VDYIx'><center id='VDYIx'></center></pre></bdo></b><th id='VDYIx'></th></span></q></dt></tr></i><div id='VDYIx'><tfoot id='VDYIx'></tfoot><dl id='VDYIx'><fieldset id='VDYIx'></fieldset></dl></div>
                  <tfoot id='VDYIx'></tfoot>
                • 本文介绍了在 C++ 中获取变量地址的最便携和可靠的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果变量类型重载了 operator&(),则使用 & 获取变量的地址可能会出现问题.例如,_com_ptr_ 有 operator&() 重载,具有修改对象的副作用.

                  Using & to get an address of a variable can be problematic if the variable type has overloaded operator&(). For example, _com_ptr_ has operator&() overloaded with a side effect of modifying the object.

                  现在我有一组复杂的模板,具有如下功能:

                  Now I have a complicated set of templates with functions like this:

                  template<class T>
                  void process( const T* object )
                  {
                      //whatever
                  }    
                  
                  template<class T>
                  void tryProcess( T& object )
                  {
                      process( &object )
                  }
                  

                  tryProcess() 中,我需要获取一个 T* 指针,该指针包含 T 类型的实际对象的地址.

                  In tryProcess() I need to get a T* pointer holding the address of the actual object of type T.

                  上述 tryProcess() 的实现只有在 class T 没有重载 operator&() 时才能正常工作.因此,如果我调用 tryProcess<_com_ptr_<Interface>>() 我会得到意想不到的结果 - 重载的 operator&() 被触发.

                  The above implementation of tryProcess() will only work allright if class T doesn't have operator&() overloaded. So if I call tryProcess<_com_ptr_<Interface>>() I can get unexpected results - the overloaded operator&() is triggered.

                  在 另一个问题 以下解决方法是建议:

                  In another question the following workaround is suggested:

                  template<class T>
                  T* getAddress( T& object )
                  {
                     return reinterpret_cast<T*>( &reinterpret_cast<char&>( object ) );
                  }
                  

                  有了这样的函数,我可以实现 tryProcess() 如下:

                  With such a function I can implement tryProcess() as follows:

                  template<class T>
                  void tryProcess( T& object )
                  {
                      process( getAddress( object ) )
                  }
                  

                  并且将始终获得与 class T 是否重载 operator&() 无关的相同行为.这在 Visual C++ 7 上引入了优化的零开销 - 编译器得到要做什么并且只得到对象地址.

                  and will always get the same behavior independent of whether class T has operator&() overloaded. This introduces zero overhead with optimizations on on Visual C++ 7 - the compiler gets what to do and just gets the object address.

                  这个问题的解决方案的可移植性和标准兼容性如何?如何改进?

                  How portable and standard-compilant is this solution to the problem? How could it be improved?

                  推荐答案

                  是标准的投诉.该问题已提请 ISO C++ 委员会注意,该问题与 offsetof 实现相关的问题在此方面出现了问题.所考虑的解决方案包括收紧 POD 定义,或对要与 offsetof 一起使用的类型添加额外限制.当提出 reinterpret_cast 解决方案时,这些解决方案被拒绝了.由于这提供了一种解决问题的符合标准的方法,委员会认为没有必要向 offsetof 添加额外的要求,并保留了对实现的修复.

                  It is standard-complaint. The issue was brought to the attention of the ISO C++ committee in relation to problems with offsetof implementations that broke on this. Amongst the solutions considered were tightening the POD definition, or adding an extra restriction on types to be used with offsetof. Those solutions were rejected when the reinterpret_cast solution was brought up. Since this offered a standard-compliant way around the problem, the committee did not see a need to add extra requirements to the offsetof, and left fixes to the implementations.

                  这篇关于在 C++ 中获取变量地址的最便携和可靠的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:有没有办法在 Visual Studio 2012 中使用委托构造函数 下一篇:在代码中嵌入图像,不使用资源部分或外部图像

                  相关文章

                  最新文章

                • <tfoot id='3LfWu'></tfoot>
                  <i id='3LfWu'><tr id='3LfWu'><dt id='3LfWu'><q id='3LfWu'><span id='3LfWu'><b id='3LfWu'><form id='3LfWu'><ins id='3LfWu'></ins><ul id='3LfWu'></ul><sub id='3LfWu'></sub></form><legend id='3LfWu'></legend><bdo id='3LfWu'><pre id='3LfWu'><center id='3LfWu'></center></pre></bdo></b><th id='3LfWu'></th></span></q></dt></tr></i><div id='3LfWu'><tfoot id='3LfWu'></tfoot><dl id='3LfWu'><fieldset id='3LfWu'></fieldset></dl></div>
                    <bdo id='3LfWu'></bdo><ul id='3LfWu'></ul>
                  <legend id='3LfWu'><style id='3LfWu'><dir id='3LfWu'><q id='3LfWu'></q></dir></style></legend>

                  <small id='3LfWu'></small><noframes id='3LfWu'>