现在的位置: 首页 > 综合 > 正文

boost中模板函数addressof()实现原理

2018年07月05日 ⁄ 综合 ⁄ 共 2551字 ⁄ 字号 评论关闭

&作为取址符号用来获取对象的地址,但由于c++太灵活,通过重载operator&可以改变operator&原有的语意。

如下代码:

 

 

虽然c++支持对operator&的重载,但绝大部分情况下我们无论如何也不应该重载operator&,但是不乏不怀好意的人来重载operator&,

当你需要使用对象的真实地址时,这种情况下boost库中addressof函数满足我们的需求了:

神奇吧~~~没有做不到,只有想不到这句话比较适合c++滴~~~~~

现在我们要关注一下,这个addressof函数的实现原理是啥啊,来看下源码先:

  

以上是模板函数addressof()的实现原理,实际是一个cast trick,c++标准中有如下的规定:

An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer
to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. That is, a
reference cast reinterpret_cast<T&>(x) has the same effect as the conversion
*reinterpret_cast<T*>(&x) with the built-in & and * operators. The result is an lvalue that refers
to the same object as the source lvalue, but with a different type. No temporary is created, no copy is made,
and constructors (12.1) or conversion functions (12.3) are not called.67)  (ISO/IEC 14882:2003(E)  5.2.10 Reinterpret cast)

大体意思就是如果一个指向T1类型的指针可以通过reinterpret_cast明确的转换成一个指向T2类型的指针,那么类型为T1的左值表达式就可以强制

转化成一个T2类型的引用,也就是说cast reinterpret_cast<T&>(x)和*reinterpret_cast<T*>(&x)是等价的,前提是在内建&和*语意下。c++

标准还强调,上述的转换结果是一个左值,和被转换的源左值引用着同一个对象,只是类型不同而已,也就是说c++标准保证,如果一个T1类型的对象x,

被强制转换成了一个T2类型引用,那么T2引用是引用着T1对象,想当于*reinterpret_cast<T2*>(&x) 。

所以说addressof()函数的实现是基于上述规定的。

所以

c++保证也会返回obj的真实地址的。

 

抱歉!评论已关闭.