diff --git a/addressof_fn_test.cpp b/addressof_fn_test.cpp new file mode 100644 index 0000000..0d043bf --- /dev/null +++ b/addressof_fn_test.cpp @@ -0,0 +1,76 @@ +#include + +#if defined(BOOST_MSVC) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// addressof_fn_test.cpp: addressof( f ) +// +// Copyright (c) 2008, 2009 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include + + +void f0() +{ +} + +void f1(int) +{ +} + +void f2(int, int) +{ +} + +void f3(int, int, int) +{ +} + +void f4(int, int, int, int) +{ +} + +void f5(int, int, int, int, int) +{ +} + +void f6(int, int, int, int, int, int) +{ +} + +void f7(int, int, int, int, int, int, int) +{ +} + +void f8(int, int, int, int, int, int, int, int) +{ +} + +void f9(int, int, int, int, int, int, int, int, int) +{ +} + +int main() +{ + BOOST_TEST( boost::addressof( f0 ) == &f0 ); + BOOST_TEST( boost::addressof( f1 ) == &f1 ); + BOOST_TEST( boost::addressof( f2 ) == &f2 ); + BOOST_TEST( boost::addressof( f3 ) == &f3 ); + BOOST_TEST( boost::addressof( f4 ) == &f4 ); + BOOST_TEST( boost::addressof( f5 ) == &f5 ); + BOOST_TEST( boost::addressof( f6 ) == &f6 ); + BOOST_TEST( boost::addressof( f7 ) == &f7 ); + BOOST_TEST( boost::addressof( f8 ) == &f8 ); + BOOST_TEST( boost::addressof( f9 ) == &f9 ); + + return boost::report_errors(); +} diff --git a/addressof_test2.cpp b/addressof_test2.cpp new file mode 100644 index 0000000..b1c36f8 --- /dev/null +++ b/addressof_test2.cpp @@ -0,0 +1,95 @@ +// Copyright (C) 2002 Brad King (brad.king@kitware.com) +// Douglas Gregor (gregod@cs.rpi.edu) +// +// Copyright 2009 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// For more information, see http://www.boost.org + + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(push, 3) +#endif + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(pop) +#endif + +#include + +template void scalar_test( T * = 0 ) +{ + T* px = new T(); + + T& x = *px; + BOOST_TEST( boost::addressof(x) == px ); + + const T& cx = *px; + const T* pcx = boost::addressof(cx); + BOOST_TEST( pcx == px ); + + volatile T& vx = *px; + volatile T* pvx = boost::addressof(vx); + BOOST_TEST( pvx == px ); + + const volatile T& cvx = *px; + const volatile T* pcvx = boost::addressof(cvx); + BOOST_TEST( pcvx == px ); + + delete px; +} + +template void array_test( T * = 0 ) +{ + T nrg[3] = {1,2,3}; + T (*pnrg)[3] = &nrg; + BOOST_TEST( boost::addressof(nrg) == pnrg ); + + T const cnrg[3] = {1,2,3}; + T const (*pcnrg)[3] = &cnrg; + BOOST_TEST( boost::addressof(cnrg) == pcnrg ); +} + +class convertible { +public: + + convertible( int = 0 ) + { + } + + template operator U () const + { + return U(); + } +}; + +class convertible2 { +public: + + convertible2( int = 0 ) + { + } + + operator convertible2* () const + { + return 0; + } +}; + +int main() +{ + scalar_test(); + scalar_test(); + + array_test(); + array_test(); + + return boost::report_errors(); +} diff --git a/include/boost/utility/addressof.hpp b/include/boost/utility/addressof.hpp index 8e0a586..48602be 100644 --- a/include/boost/utility/addressof.hpp +++ b/include/boost/utility/addressof.hpp @@ -21,6 +21,14 @@ namespace boost namespace detail { +template struct addr_impl_ref +{ + T & v_; + + inline addr_impl_ref( T & v ): v_( v ) {} + inline operator T& () const { return v_; } +}; + template struct addressof_impl { static inline T * f( T & v, long ) @@ -39,7 +47,15 @@ template struct addressof_impl template T * addressof( T & v ) { +#if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x610 ) ) + return boost::detail::addressof_impl::f( v, 0 ); + +#else + + return boost::detail::addressof_impl::f( boost::detail::addr_impl_ref( v ), 0 ); + +#endif } // Borland doesn't like casting an array reference to a char reference diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 321060c..3111cad 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -11,7 +11,9 @@ import testing ; # Please keep the tests ordered by filename test-suite utility : + [ run ../addressof_fn_test.cpp ] [ run ../addressof_test.cpp ] + [ run ../addressof_test2.cpp ] [ run ../assert_test.cpp ] [ run ../base_from_member_test.cpp ] [ run ../binary_search_test.cpp ]