Add free function to_address

This commit is contained in:
Glen Fernandes
2017-07-21 06:21:50 -04:00
parent 2876914d02
commit ac6044769f
3 changed files with 78 additions and 0 deletions

View File

@ -239,6 +239,20 @@ struct pointer_traits<T*> {
};
#endif
template<class T>
inline typename pointer_traits<T>::element_type*
to_address(const T& v) BOOST_NOEXCEPT
{
return pointer_traits<T>::to_address(v);
}
template<class T>
inline T*
to_address(T* v) BOOST_NOEXCEPT
{
return v;
}
} /* boost */
#endif

View File

@ -116,6 +116,7 @@ run pointer_traits_element_type_test.cpp ;
run pointer_traits_difference_type_test.cpp ;
run pointer_traits_rebind_test.cpp ;
run pointer_traits_pointer_to_test.cpp ;
run to_address_test.cpp ;
use-project /boost/core/swap : ./swap ;
build-project ./swap ;

63
test/to_address_test.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/pointer_traits.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
class pointer {
public:
typedef typename boost::pointer_traits<T>::element_type element_type;
pointer(T value)
: value_(value) { }
T operator->() const BOOST_NOEXCEPT {
return value_;
}
private:
T value_;
};
int main()
{
int i = 0;
{
typedef int* type;
type p = &i;
BOOST_TEST(boost::to_address(p) == &i);
}
{
typedef pointer<int*> type;
type p(&i);
BOOST_TEST(boost::to_address(p) == &i);
}
{
typedef pointer<pointer<int*> > type;
type p(&i);
BOOST_TEST(boost::to_address(p) == &i);
}
{
typedef void* type;
type p = &i;
BOOST_TEST(boost::to_address(p) == &i);
}
{
typedef pointer<void*> type;
type p(&i);
BOOST_TEST(boost::to_address(p) == &i);
}
{
typedef const int* type;
type p = &i;
BOOST_TEST(boost::to_address(p) == &i);
}
{
typedef pointer<const int*> type;
type p(&i);
BOOST_TEST(boost::to_address(p) == &i);
}
return boost::report_errors();
}