mirror of
https://github.com/boostorg/core.git
synced 2025-07-29 12:27:42 +02:00
Add free function to_address
This commit is contained in:
@ -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
|
||||
|
@ -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
63
test/to_address_test.cpp
Normal 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();
|
||||
}
|
Reference in New Issue
Block a user