Make pointer_traits SFINAE friendly

This commit is contained in:
Glen Fernandes 2021-12-12 19:03:57 -05:00
parent 6da7958281
commit 0e62373aa2
7 changed files with 397 additions and 150 deletions

View File

@ -1,5 +1,5 @@
[/
Copyright 2017-2018 Glen Joseph Fernandes
Copyright 2017-2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
@ -47,68 +47,87 @@ void function(Allocator& a)
```
namespace boost {
template<class T> struct pointer_traits {
template<class T>
struct pointer_traits {
typedef T pointer;
typedef ``['see below]`` element_type;
typedef ``['see below]`` difference_type;
template<class U> struct rebind_to { typedef ``['see below]`` type; };
template<class U> using rebind = typename rebind_to<U>::type;
template<class U>
struct rebind_to {
typedef ``['see below]`` type;
};
static pointer pointer_to(``['see below]`` v);
};
template<class U>
using rebind = typename rebind_to<U>::type;
template<class T> struct pointer_traits<T*> {
static pointer pointer_to(element_type& v);
};
template<class T>
struct pointer_traits<T*> {
typedef T* pointer;
typedef T element_type;
typedef std::ptrdiff_t difference_type;
template<class U> struct rebind_to { typedef U* type; };
template<class U> using rebind = typename rebind_to<U>::type;
template<class U>
struct rebind_to {
typedef U* type;
};
template<class U>
using rebind = typename rebind_to<U>::type;
static pointer pointer_to(``['see below]`` v) noexcept;
};
};
template<class T>
constexpr T* to_address(T* v) noexcept;
template<class T>
constexpr T* to_address(T* v) noexcept;
template<class T>
auto to_address(const T& v) noexcept;
}
template<class T>
auto to_address(const T& v) noexcept;
} // boost
```
[section Overview]
If the member type `element_type` is not defined, then all other members are
also not defined (`pointer_traits` is SFINAE-friendly).
[endsect]
[section Member types]
[variablelist
[[`typedef` ['see below] `element_type;`]
[`T::element_type` if such a type exists; otherwise `U` if `T` is a class
template instantiation of the form `Pointer<U, Args>`, where `Args` is zero
or more type arguments; otherwise the specialization is ill-formed.]]
or more type arguments; otherwise the member is not defined.]]
[[`typedef` ['see below] `difference_type;`]
[`T::difference_type` if such a type exists; otherwise `std::ptrdiff_t`.]]
[[`template<class U> struct rebind_to { typedef` ['see below] `type; };`]
[`type` is `T::rebind<U>` if such a type exists; otherwise, `Pointer<V, Args>`
if `T` is a class template instantiation of the form `Pointer<T, Args>`,
where `Args` is zero or more type arguments; otherwise, the instantiation of
`rebind_to` is ill-formed.]]]
where `Args` is zero or more type arguments; otherwise, the member is not
defined.]]]
[endsect]
[section Member functions]
[variablelist
[[`static pointer pointer_traits::pointer_to(`['see below] `v);`]
[[`static pointer pointer_traits::pointer_to(element_type& v);`]
[[variablelist
[[Remark]
[If `element_type` is a void type, the type of `v` is unspecified; otherwise,
it is `element_type&`.]]
[If `element_type` is a void type, this member is not defined.]]
[[Returns]
[A pointer to `v` obtained by calling `T::pointer_to(v)`.]]]]]
[[`static pointer pointer_traits<T*>::pointer_to(`['see below] `v) noexcept;`]
[[`static pointer pointer_traits<T*>::pointer_to(element_type& v) noexcept;`]
[[variablelist
[[Remark]
[If `element_type` is a void type, the type of `v` is unspecified; otherwise,
it is `element_type&`.]]
[If `element_type` is a void type, this member is not defined.]]
[[Returns][`addressof(v)`.]]]]]]
[endsect]

View File

@ -1,5 +1,5 @@
/*
Copyright 2017-2018 Glen Joseph Fernandes
Copyright 2017-2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
@ -9,43 +9,23 @@ Distributed under the Boost Software License, Version 1.0.
#define BOOST_CORE_POINTER_TRAITS_HPP
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
#include <memory>
#else
#include <boost/core/addressof.hpp>
#include <cstddef>
#endif
namespace boost {
#if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
template<class T>
struct pointer_traits
: std::pointer_traits<T> {
template<class U>
struct rebind_to {
typedef typename std::pointer_traits<T>::template rebind<U> type;
};
};
template<class T>
struct pointer_traits<T*>
: std::pointer_traits<T*> {
template<class U>
struct rebind_to {
typedef U* type;
};
};
#else
namespace detail {
struct ptr_none { };
template<class>
struct ptr_void {
struct ptr_valid {
typedef void type;
};
template<class T>
struct ptr_first;
template<class>
struct ptr_first {
typedef ptr_none type;
};
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args>
@ -75,7 +55,7 @@ struct ptr_element {
};
template<class T>
struct ptr_element<T, typename ptr_void<typename T::element_type>::type> {
struct ptr_element<T, typename ptr_valid<typename T::element_type>::type> {
typedef typename T::element_type type;
};
@ -86,12 +66,12 @@ struct ptr_difference {
template<class T>
struct ptr_difference<T,
typename ptr_void<typename T::difference_type>::type> {
typename ptr_valid<typename T::difference_type>::type> {
typedef typename T::difference_type type;
};
template<class T, class V>
struct ptr_transform;
template<class, class>
struct ptr_transform { };
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args, class V>
@ -117,68 +97,100 @@ struct ptr_transform<T<U1, U2, U3>, V> {
#endif
template<class T, class U, class = void>
struct ptr_rebind {
typedef typename ptr_transform<T, U>::type type;
};
struct ptr_rebind
: ptr_transform<T, U> { };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class T, class U>
struct ptr_rebind<T, U,
typename ptr_void<typename T::template rebind<U> >::type> {
typename ptr_valid<typename T::template rebind<U> >::type> {
typedef typename T::template rebind<U> type;
};
#endif
template<class T>
struct ptr_value {
typedef T type;
template<class>
struct ptr_void {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<>
struct ptr_value<void> {
typedef struct { } type;
struct ptr_void<void> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<>
struct ptr_void<const void> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<>
struct ptr_void<volatile void> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<>
struct ptr_void<const volatile void> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<class, class E, bool = ptr_void<E>::value>
struct ptr_to { };
template<class T, class E>
struct ptr_to<T, E, false> {
static T pointer_to(E& v) {
return T::pointer_to(v);
}
};
template<class T>
struct ptr_to<T*, T, false> {
static T* pointer_to(T& v) BOOST_NOEXCEPT {
return boost::addressof(v);
}
};
template<class T, class E>
struct ptr_traits
: ptr_to<T, E> {
typedef T pointer;
typedef E element_type;
typedef typename ptr_difference<T>::type difference_type;
template<class U>
struct rebind_to
: ptr_rebind<T, U> { };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class U>
using rebind = typename rebind_to<U>::type;
#endif
};
template<class T>
struct ptr_traits<T, ptr_none> { };
} /* detail */
template<class T>
struct pointer_traits {
typedef T pointer;
typedef typename detail::ptr_element<T>::type element_type;
typedef typename detail::ptr_difference<T>::type difference_type;
template<class U>
struct rebind_to {
typedef typename detail::ptr_rebind<T, U>::type type;
};
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class U>
using rebind = typename detail::ptr_rebind<T, U>::type;
#endif
static pointer
pointer_to(typename detail::ptr_value<element_type>::type& v) {
return pointer::pointer_to(v);
}
};
struct pointer_traits
: detail::ptr_traits<T, typename detail::ptr_element<T>::type> { };
template<class T>
struct pointer_traits<T*> {
struct pointer_traits<T*>
: detail::ptr_to<T*, T> {
typedef T* pointer;
typedef T element_type;
typedef std::ptrdiff_t difference_type;
template<class U>
struct rebind_to {
typedef U* type;
};
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class U>
using rebind = U*;
using rebind = typename rebind_to<U>::type*;
#endif
static T*
pointer_to(typename detail::ptr_value<T>::type& v) BOOST_NOEXCEPT {
return boost::addressof(v);
}
};
#endif
template<class T>
BOOST_CONSTEXPR inline T*

View File

@ -189,6 +189,9 @@ 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 pointer_traits_sfinae_test.cpp ;
run pointer_traits_rebind_sfinae_test.cpp ;
run pointer_traits_pointer_to_sfinae_test.cpp ;
run to_address_test.cpp ;
run exchange_test.cpp ;

View File

@ -0,0 +1,75 @@
/*
Copyright 2021 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/config.hpp>
#if !defined(BOOST_NO_CXX11_DECLTYPE_N3276) || !defined(BOOST_MSVC)
#include <boost/core/pointer_traits.hpp>
#include <boost/core/lightweight_test.hpp>
#if !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
template<class T>
class has_pointer_to {
template<class>
struct result {
char one;
char two;
};
template<class O>
static auto check(int) -> result<decltype(O::pointer_to)>;
template<class O>
static char check(long);
public:
static const bool value = sizeof(check<T>(0)) != 1;
};
#else
template<class T>
class has_pointer_to {
template<int>
struct result {
char one;
char two;
};
template<class O>
static result<sizeof(&O::pointer_to)> check(int);
template<class>
static char check(long);
public:
static const bool value = sizeof(check<T>(0)) != 1;
};
#endif
struct P1 { };
struct P2 {
typedef int element_type;
};
struct P3 {
typedef void element_type;
};
int main()
{
BOOST_TEST((!has_pointer_to<boost::pointer_traits<P1> >::value));
BOOST_TEST(has_pointer_to<boost::pointer_traits<P2> >::value);
BOOST_TEST(!has_pointer_to<boost::pointer_traits<P3> >::value);
BOOST_TEST(has_pointer_to<boost::pointer_traits<int*> >::value);
BOOST_TEST(!has_pointer_to<boost::pointer_traits<void*> >::value);
return boost::report_errors();
}
#else
int main()
{
return 0;
}
#endif

View File

@ -0,0 +1,83 @@
/*
Copyright 2021 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/config.hpp>
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
(!defined(BOOST_GCC) || (BOOST_GCC >= 40800)) && \
(!defined(BOOST_MSVC) || (BOOST_MSVC >= 1900))
#include <boost/core/pointer_traits.hpp>
#include <boost/core/lightweight_test.hpp>
template<class>
struct valid {
typedef void type;
};
#if !defined(BOOST_GCC) || (BOOST_GCC >= 50000)
template<class, class, class = void>
struct has_rebind {
static const bool value = false;
};
template<class T, class U>
struct has_rebind<T, U,
typename valid<typename T::template rebind<U> >::type> {
static const bool value = true;
};
#else
template<template<class> class T, class U>
using defer = T<U>;
template<class T, class U>
using rebind = defer<T::template rebind, U>;
template<class, class, template<class, class> class = rebind, class = void>
struct has_rebind {
static const bool value = false;
};
template<class T, class U, template<class, class> class R>
struct has_rebind<T, U, R, typename valid<R<T, U> >::type> {
static const bool value = true;
};
#endif
struct P1 { };
struct P2 {
typedef int element_type;
};
struct P3 {
typedef int element_type;
template<class>
struct rebind { };
};
template<class T>
struct S {
typedef T element_type;
};
typedef S<int> P4;
int main()
{
BOOST_TEST((!has_rebind<boost::pointer_traits<P1>, char>::value));
BOOST_TEST((!has_rebind<boost::pointer_traits<P2>, char>::value));
BOOST_TEST((has_rebind<boost::pointer_traits<P3>, char>::value));
BOOST_TEST((has_rebind<boost::pointer_traits<P4>, char>::value));
BOOST_TEST((has_rebind<boost::pointer_traits<int*>, char>::value));
return boost::report_errors();
}
#else
int main()
{
return 0;
}
#endif

View File

@ -0,0 +1,101 @@
/*
Copyright 2021 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>
struct valid {
typedef void type;
};
template<class, class = void>
struct has_pointer {
static const bool value = false;
};
template<class T>
struct has_pointer<T, typename valid<typename T::pointer>::type> {
static const bool value = true;
};
template<class, class = void>
struct has_element_type {
static const bool value = false;
};
template<class T>
struct has_element_type<T, typename valid<typename T::element_type>::type> {
static const bool value = true;
};
template<class, class = void>
struct has_difference_type {
static const bool value = false;
};
template<class T>
struct has_difference_type<T,
typename valid<typename T::difference_type>::type> {
static const bool value = true;
};
template<class, class, class = void>
struct has_rebind_to_type {
static const bool value = false;
};
template<class T, class U>
struct has_rebind_to_type<T, U,
typename valid<typename T::template rebind_to<U>::type>::type> {
static const bool value = true;
};
struct P1 { };
struct P2 {
typedef int element_type;
};
struct P3 {
typedef int element_type;
template<class>
struct rebind { };
};
template<class T>
struct S {
typedef T element_type;
};
typedef S<int> P4;
int main()
{
BOOST_TEST(!has_pointer<boost::pointer_traits<P1> >::value);
BOOST_TEST(!has_element_type<boost::pointer_traits<P1> >::value);
BOOST_TEST(!has_difference_type<boost::pointer_traits<P1> >::value);
BOOST_TEST((!has_rebind_to_type<boost::pointer_traits<P1>, char>::value));
BOOST_TEST(has_pointer<boost::pointer_traits<P2> >::value);
BOOST_TEST(has_element_type<boost::pointer_traits<P2> >::value);
BOOST_TEST(has_difference_type<boost::pointer_traits<P2> >::value);
BOOST_TEST((!has_rebind_to_type<boost::pointer_traits<P2>, char>::value));
BOOST_TEST(has_pointer<boost::pointer_traits<P3> >::value);
BOOST_TEST(has_element_type<boost::pointer_traits<P3> >::value);
BOOST_TEST(has_difference_type<boost::pointer_traits<P3> >::value);
BOOST_TEST((has_rebind_to_type<boost::pointer_traits<P3>, char>::value));
BOOST_TEST(has_pointer<boost::pointer_traits<P4> >::value);
BOOST_TEST(has_element_type<boost::pointer_traits<P4> >::value);
BOOST_TEST(has_difference_type<boost::pointer_traits<P4> >::value);
BOOST_TEST((has_rebind_to_type<boost::pointer_traits<P4>, char>::value));
BOOST_TEST(has_pointer<boost::pointer_traits<int*> >::value);
BOOST_TEST(has_element_type<boost::pointer_traits<int*> >::value);
BOOST_TEST(has_difference_type<boost::pointer_traits<int*> >::value);
BOOST_TEST((has_rebind_to_type<boost::pointer_traits<int*>, char>::value));
return boost::report_errors();
}

View File

@ -13,9 +13,11 @@ class P1 {
public:
explicit P1(T* p)
: p_(p) { }
T* operator->() const BOOST_NOEXCEPT {
return p_;
}
private:
T* p_;
};
@ -25,9 +27,11 @@ class P2 {
public:
explicit P2(T* p)
: p_(p) { }
P1<T> operator->() const BOOST_NOEXCEPT {
return p_;
}
private:
P1<T> p_;
};
@ -38,20 +42,24 @@ class P3 {
public:
explicit P3(T* p)
: p_(p) { }
T* get() const BOOST_NOEXCEPT {
return p_;
}
private:
T* p_;
};
namespace boost {
template<class T>
struct pointer_traits<P3<T> > {
static T* to_address(const P3<T>& p) BOOST_NOEXCEPT {
return p.get();
}
};
} /* boost */
template<class T>
@ -59,77 +67,29 @@ class P4 {
public:
explicit P4(T* p)
: p_(p) { }
T* operator->() const BOOST_NOEXCEPT {
return 0;
}
T* get() const BOOST_NOEXCEPT {
return p_;
}
private:
int* p_;
};
namespace boost {
template<class T>
struct pointer_traits<P4<T> > {
static T* to_address(const P4<T>& p) BOOST_NOEXCEPT {
return p.get();
}
};
} /* boost */
#if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
template<class T>
class P5 {
public:
explicit P5(T* p)
: p_(p) { }
T* get() const BOOST_NOEXCEPT {
return p_;
}
private:
T* p_;
};
namespace std {
template<class T>
struct pointer_traits<P5<T> > {
static T* to_address(const P5<T>& p) BOOST_NOEXCEPT {
return p.get();
}
};
} /* std */
template<class T>
class P6 {
public:
explicit P6(T* p)
: p_(p) { }
T* get() const BOOST_NOEXCEPT {
return p_;
}
private:
T* p_;
};
namespace boost {
template<class T>
struct pointer_traits<P6<T> > {
static T* to_address(const P6<T>& p) BOOST_NOEXCEPT {
return p.get();
}
};
} /* boost */
namespace std {
template<class T>
struct pointer_traits<P6<T> > {
static T* to_address(const P6<T>& /*p*/) BOOST_NOEXCEPT {
return 0;
}
};
} /* std */
#endif
#endif
int main()
@ -147,12 +107,6 @@ int main()
BOOST_TEST(boost::to_address(p3) == &i);
P4<int> p4(&i);
BOOST_TEST(boost::to_address(p4) == &i);
#if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
P5<int> p5(&i);
BOOST_TEST(boost::to_address(p5) == &i);
P6<int> p6(&i);
BOOST_TEST(boost::to_address(p6) == &i);
#endif
#endif
return boost::report_errors();
}