最便携和可靠的方式获取C ++中变量的地址 [英] Most portable and reliable way to get the address of variable in C++

查看:72
本文介绍了最便携和可靠的方式获取C ++中变量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在我有一个复杂的模板集,这个:

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

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

tryProcess()我需要一个 T * tryProcess()的实现对象的实际对象的地址 T



<如果类T 没有运算符&()超载,则
所以如果我调用 tryProcess< _com_ptr_< Interface>>()我可以得到意想不到的结果 - 重载的运算符&



另一个问题以下解决方法是
$

b $ b

 模板< class T> 
T * getAddress(T& object)
{
return reinterpret_cast< T *>(& reinterpret_cast< char&>(object));使用这样一个函数,我可以实现 tryProcess()
}


$ b <
如下:

 模板< class T& 
void tryProcess(T& object)
{
process(getAddress(object))
}

并且将始终获得相同的行为,而与类T 是否具有运算符& c $ c>重载。这在Visual C ++ 7上的优化上引入了零开销 - 编译器得到了什么,只是获取对象地址。



如何便携和标准编译器是这个解决方案问题?如何改进?

解决方案

这是标准投诉。该问题已提请ISO C ++委员会注意关于 offsetof 实现的问题。在所考虑的解决方案之中是紧缩POD定义,或者对要与 offsetof 一起使用的类型添加额外的限制。当 reinterpret_cast 解决方案提出时,这些解决方案被拒绝。由于这提供了一个符合标准的方式解决问题,委员会没有看到需要添加额外的要求到 offsetof ,并留下修复的实施。


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 )
}

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

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 ) );
}

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

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

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?

解决方案

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 ++中变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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