From ac6044769f2ec9ccd83fe8b85a8e058abef136b6 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Fri, 21 Jul 2017 06:21:50 -0400 Subject: [PATCH] Add free function to_address --- include/boost/core/pointer_traits.hpp | 14 ++++++ test/Jamfile.v2 | 1 + test/to_address_test.cpp | 63 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test/to_address_test.cpp diff --git a/include/boost/core/pointer_traits.hpp b/include/boost/core/pointer_traits.hpp index 5cce489..b604b58 100644 --- a/include/boost/core/pointer_traits.hpp +++ b/include/boost/core/pointer_traits.hpp @@ -239,6 +239,20 @@ struct pointer_traits { }; #endif +template +inline typename pointer_traits::element_type* +to_address(const T& v) BOOST_NOEXCEPT +{ + return pointer_traits::to_address(v); +} + +template +inline T* +to_address(T* v) BOOST_NOEXCEPT +{ + return v; +} + } /* boost */ #endif diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 204e79e..024fab6 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 ; diff --git a/test/to_address_test.cpp b/test/to_address_test.cpp new file mode 100644 index 0000000..a4fdfd2 --- /dev/null +++ b/test/to_address_test.cpp @@ -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 +#include + +template +class pointer { +public: + typedef typename boost::pointer_traits::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 type; + type p(&i); + BOOST_TEST(boost::to_address(p) == &i); + } + { + typedef pointer > 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 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 type; + type p(&i); + BOOST_TEST(boost::to_address(p) == &i); + } + return boost::report_errors(); +}