|
Boost Users : |
Subject: [Boost-users] boost::addressof
From: StanisÅaw Findeisen (stf_at_[hidden])
Date: 2010-08-31 17:11:59
Hi!
In the latest boost release (1.44) boost::addressof is defined as
follows (boost/utility/addressof.hpp):
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_; }
private:
addr_impl_ref & operator=(const addr_impl_ref &);
};
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)));
}
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 );
#endif
}
Q1: What is volatile in the cast needed for?
Q2: Could anyone please explain to me what are the 2nd parameters in
each of the f functions in struct addressof_impl for?
Q3: What is struct addr_impl_ref::operator T& () const ??! I understand
it is there to get called when passing addr_impl_ref<T>(v) instead of v
to addressof_impl<T>::f, but I've never seen such a thing before. I
would understand T& struct addr_impl_ref::operator(), or something, but
this one is different. Sorry if this is a newbie question.
Q4: I can see that in 1.34.1 boost::addressof was much more simple:
// VC7 strips const from nested classes unless we add indirection here
# if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
template<class T> struct _addp
{
typedef T * type;
};
template <typename T> typename _addp<T>::type
# else
template <typename T> T*
# endif
addressof(T& v)
{
return reinterpret_cast<T*>(
&const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
}
so I wonder what was the problem with this design?...
-- http://eisenbits.homelinux.net/~stf/ OpenPGP: DFD9 0146 3794 9CF6 17EA D63F DBF5 8AA8 3B31 FE8A Like Hardship, Risk & Challenge? --- Follow Jesus!!
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net