?
博客舊址
?
?
延伸讀物:
?
個(gè)人卓見(jiàn):
?此人擅長(zhǎng)邏輯推理與逐層剖析,總結(jié)一般化方,評(píng)論淺顯易懂,條理清晰,深入淺處,謙虛話語(yǔ)中充滿(mǎn)了智慧。
?
signal slot manager slot connection base_connection
?
?
?
cast trick
namespace boost??
{??
?
namespace detail??
{??
?
template<class T> struct addr_impl_ref??
{??
??? T & v_;??
?
??? inline addr_impl_ref( T & v ): v_( v ) {}??
??? inline operator T& () const { return v_; }??
};??
?
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)));??
??? //這是關(guān)鍵的地方,先強(qiáng)制轉(zhuǎn)換成const volatile char &,然后去掉const屬性,再取址,避免調(diào)用v的重載的operator&??
??? }??
?
??? static inline T * f( T * v, int )??
??? {??
??????? return v;??
??? }??
};??
?
} // namespace detail??
?
template<class T> T * addressof( T & v )??
{?
#if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x610 ) )??
?
??? return boost::detail::addressof_impl<T>::f( v, 0 );?
?
#else??
?
??? return boost::detail::addressof_impl<T>::f( boost::detail::addr_impl_ref<T>( v ), 0 );//vc平臺(tái)走這個(gè)分支,??
??? //調(diào)用addr_impl_ref<T>的構(gòu)造函數(shù)創(chuàng)建一個(gè)addr_impl_ref<T>對(duì)象,??
??? //由于boost::detail::addressof_impl<T>::f( T & v, long ),所以??
??? //addr_impl_ref<T>對(duì)象要被強(qiáng)制轉(zhuǎn)換成T&,觸發(fā)addr_impl_ref重載的??
??? //T& (),最終返回v的引用。?
??????????????????????????????????????????????????????
#endif??
}?
?
#endif // BOOST_UTILITY_ADDRESSOF_HPP?
c++標(biāo)準(zhǔn)中有如下的規(guī)定:
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? or conversion functionsare not called.)? (ISO/IEC 14882:2003(E)? 5.2.10 Reinterpret cast)
大體意思就是如果一個(gè)指向T1類(lèi)型的指針可以通過(guò)reinterpret_cast明確的轉(zhuǎn)換成一個(gè)指向T2類(lèi)型的指針,那么類(lèi)型為T(mén)1的左值表達(dá)式就可以強(qiáng)制
轉(zhuǎn)化成一個(gè)T2類(lèi)型的引用,也就是說(shuō)cast reinterpret_cast<T&>(x)和*reinterpret_cast<T*>(&x)是等價(jià)的,前提是在內(nèi)建&和*語(yǔ)意下。c++
標(biāo)準(zhǔn)還強(qiáng)調(diào),上述的轉(zhuǎn)換結(jié)果是一個(gè)左值,和被轉(zhuǎn)換的源左值引用著同一個(gè)對(duì)象,只是類(lèi)型不同而已,也就是說(shuō)c++標(biāo)準(zhǔn)保證,如果一個(gè)T1類(lèi)型的對(duì)象x,被強(qiáng)制轉(zhuǎn)換成了一個(gè)T2類(lèi)型引用,那么T2引用是引用著T1對(duì)象,想當(dāng)于*reinterpret_cast<T2*>(&x)。
所以說(shuō)addressof()函數(shù)的實(shí)現(xiàn)是基于上述規(guī)定的。
?
?
?
base from member idiom
當(dāng)基類(lèi)初始化需要派生類(lèi)的類(lèi)成員變量時(shí)候,base classes are?initialized in order of declaration. So moving the desired member to another base class, that is initialized before the desired base class, can ensure proper initialization.
rationale:
?
template <class MemberType, int UniqueId=0>
class boost::base_from_member
{
??? protected:
????????MemberType member;
//derived class?member class needed for base class initialization, extracted and wrapper within base_from_member
??????? base_from_member();
??????? template <typename T1>?explicit base_from_member(T1 x1);// param needed for MemberType class initialization.
//...
??????? template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
??????? explicit base_from_member(T1 x1, T2 x2,...T10 x10);
};
?
A type that consists of nothing but Plain Old Data.
A POD type is a C++ type that has an equivalent in C, and that uses the same rules as C uses for initialization, copying, layout, and addressing.
As an example, the C declaration struct Fred x; does not initialize the members of the Fred variable x. To make this same behavior happen in C++, Fred would need to not have any constructors. Similarly to make the C++ version of copying the same as the C version, the C++ Fred must not have overloaded the assignment operator. To make sure the other rules match, the C++ version must not have virtual functions, base classes, non-static members that are private or protected, or a destructor. It can, however, have static data members, static member functions, and non-static non-virtual member functions.
The actual definition of a POD type is recursive and gets a little gnarly. Here's a slightly simplified definition of POD: a POD type's non-static data members must be public and can be of any of these types: bool, any numeric type including the various char variants, any enumeration type, any data-pointer type (that is, any type convertible to void*), any pointer-to-function type, or any POD type, including arrays of any of these. Note: data-pointers and pointers-to-function are okay, but are not. Also note that references are not allowed. In addition, a POD type can't have constructors, virtual functions, base classes, or an overloaded assignment operator.
?
?
?
因篇幅問(wèn)題不能全部顯示,請(qǐng)點(diǎn)此查看更多更全內(nèi)容
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)