mirror of
https://github.com/boostorg/core.git
synced 2025-07-30 04:47:24 +02:00
Add free function to_address
This commit is contained in:
@ -24,7 +24,7 @@ namespace detail {
|
|||||||
|
|
||||||
template<class U>
|
template<class U>
|
||||||
inline typename boost::pointer_traits<U>::element_type*
|
inline typename boost::pointer_traits<U>::element_type*
|
||||||
ptr_traits_address(U v) BOOST_NOEXCEPT
|
ptr_traits_address(const U& v) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
return boost::pointer_traits<U>::to_address(v);
|
return boost::pointer_traits<U>::to_address(v);
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ struct pointer_traits
|
|||||||
typedef typename std::pointer_traits<T>::template rebind<U> type;
|
typedef typename std::pointer_traits<T>::template rebind<U> type;
|
||||||
};
|
};
|
||||||
static typename std::pointer_traits<T>::element_type*
|
static typename std::pointer_traits<T>::element_type*
|
||||||
to_address(T v) BOOST_NOEXCEPT {
|
to_address(const T& v) BOOST_NOEXCEPT {
|
||||||
return detail::ptr_traits_address(v.operator->());
|
return detail::ptr_traits_address(v.operator->());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -211,7 +211,7 @@ struct pointer_traits {
|
|||||||
pointer_to(typename detail::ptr_traits_value<element_type>::type& v) {
|
pointer_to(typename detail::ptr_traits_value<element_type>::type& v) {
|
||||||
return pointer::pointer_to(v);
|
return pointer::pointer_to(v);
|
||||||
}
|
}
|
||||||
static element_type* to_address(pointer v) BOOST_NOEXCEPT {
|
static element_type* to_address(const pointer& v) BOOST_NOEXCEPT {
|
||||||
return detail::ptr_traits_address(v.operator->());
|
return detail::ptr_traits_address(v.operator->());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -21,6 +21,44 @@ private:
|
|||||||
T value_;
|
T value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class special {
|
||||||
|
public:
|
||||||
|
special(T* value)
|
||||||
|
: value_(value) { }
|
||||||
|
T* get() const BOOST_NOEXCEPT {
|
||||||
|
return value_;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
T* value_;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
struct pointer_traits<special<T> > {
|
||||||
|
typedef special<T> pointer;
|
||||||
|
typedef T element_type;
|
||||||
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
template<class U>
|
||||||
|
struct rebind_to {
|
||||||
|
typedef special<U> type;
|
||||||
|
};
|
||||||
|
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
|
||||||
|
template<class U>
|
||||||
|
using rebind = typename rebind_to<U>::type;
|
||||||
|
#endif
|
||||||
|
template<class U>
|
||||||
|
static pointer pointer_to(U& v) BOOST_NOEXCEPT {
|
||||||
|
return pointer(&v);
|
||||||
|
}
|
||||||
|
static element_type* to_address(const pointer& v) BOOST_NOEXCEPT {
|
||||||
|
return v.get();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} /* boost */
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -59,5 +97,35 @@ int main()
|
|||||||
type p(&i);
|
type p(&i);
|
||||||
BOOST_TEST(boost::to_address(p) == &i);
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
typedef special<int> type;
|
||||||
|
type p(&i);
|
||||||
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
typedef special<void> type;
|
||||||
|
type p(&i);
|
||||||
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
typedef special<const int> type;
|
||||||
|
type p(&i);
|
||||||
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
typedef pointer<special<int> > type;
|
||||||
|
type p(&i);
|
||||||
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
typedef pointer<special<void> > type;
|
||||||
|
type p(&i);
|
||||||
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
typedef pointer<special<const int> > type;
|
||||||
|
type p(&i);
|
||||||
|
BOOST_TEST(boost::to_address(p) == &i);
|
||||||
|
}
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user