Fix addressof for nullptr_t values. Fixes #5487.

This commit is contained in:
Peter Dimov 2013-12-11 01:57:20 +02:00
parent d595357b41
commit 50eafe2027
3 changed files with 107 additions and 1 deletions

50
addressof_np_test.cpp Normal file
View File

@ -0,0 +1,50 @@
//
// Copyright 2013 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 <boost/utility/addressof.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cstddef>
#if defined( BOOST_NO_CXX11_NULLPTR )
void nullptr_test()
{
}
#else
void nullptr_test()
{
{
auto x = nullptr;
BOOST_TEST( boost::addressof(x) == &x );
}
{
auto const x = nullptr;
BOOST_TEST( boost::addressof(x) == &x );
}
{
auto volatile x = nullptr;
BOOST_TEST( boost::addressof(x) == &x );
}
{
auto const volatile x = nullptr;
BOOST_TEST( boost::addressof(x) == &x );
}
}
#endif
int main()
{
nullptr_test();
return boost::report_errors();
}

View File

@ -1,7 +1,7 @@
// Copyright (C) 2002 Brad King (brad.king@kitware.com)
// Douglas Gregor (gregod@cs.rpi.edu)
//
// Copyright (C) 2002, 2008 Peter Dimov
// Copyright (C) 2002, 2008, 2013 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
@ -14,6 +14,7 @@
# include <boost/config.hpp>
# include <boost/detail/workaround.hpp>
# include <cstddef>
namespace boost
{
@ -46,6 +47,60 @@ template<class T> struct addressof_impl
}
};
#if !defined( BOOST_NO_CXX11_NULLPTR )
#if defined( __clang__ ) && !defined( _LIBCPP_VERSION ) && !defined( BOOST_NO_CXX11_DECLTYPE )
typedef decltype(nullptr) addr_nullptr_t;
#else
typedef std::nullptr_t addr_nullptr_t;
#endif
template<> struct addressof_impl< addr_nullptr_t >
{
typedef addr_nullptr_t T;
static BOOST_FORCEINLINE T * f( T & v, int )
{
return &v;
}
};
template<> struct addressof_impl< addr_nullptr_t const >
{
typedef addr_nullptr_t const T;
static BOOST_FORCEINLINE T * f( T & v, int )
{
return &v;
}
};
template<> struct addressof_impl< addr_nullptr_t volatile >
{
typedef addr_nullptr_t volatile T;
static BOOST_FORCEINLINE T * f( T & v, int )
{
return &v;
}
};
template<> struct addressof_impl< addr_nullptr_t const volatile >
{
typedef addr_nullptr_t const volatile T;
static BOOST_FORCEINLINE T * f( T & v, int )
{
return &v;
}
};
#endif
} // namespace detail
template<class T>

View File

@ -17,6 +17,7 @@ alias unit_test_framework
test-suite utility
:
[ run ../addressof_fn_test.cpp ]
[ run ../addressof_np_test.cpp ]
[ run ../addressof_test.cpp ]
[ run ../addressof_test2.cpp ]
[ run ../assert_test.cpp ]