Make pointer_to SFINAE friendlier

This commit is contained in:
Glen Fernandes
2021-12-14 23:56:22 -05:00
parent a121ab0278
commit 78dd0cce2f
2 changed files with 70 additions and 42 deletions

View File

@ -106,43 +106,74 @@ struct ptr_rebind<T, U,
typedef typename T::template rebind<U> type;
};
template<class>
struct ptr_void {
#if !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
template<class T, class E>
class ptr_to_expr {
template<class>
struct result {
char x, y;
};
static E& source();
template<class O>
static auto check(int) -> result<decltype(O::pointer_to(source()))>;
template<class>
static char check(long);
public:
BOOST_STATIC_CONSTEXPR bool value = sizeof(check<T>(0)) != 1;
};
template<class T, class E>
struct ptr_to_expr<T*, E> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<class T, class E>
struct ptr_has_to {
BOOST_STATIC_CONSTEXPR bool value = ptr_to_expr<T, E>::value;
};
#else
template<class, class>
struct ptr_has_to {
BOOST_STATIC_CONSTEXPR bool value = true;
};
#endif
template<class T>
struct ptr_has_to<T, void> {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<>
struct ptr_void<void> {
BOOST_STATIC_CONSTEXPR bool value = true;
template<class T>
struct ptr_has_to<T, const void> {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<>
struct ptr_void<const void> {
BOOST_STATIC_CONSTEXPR bool value = true;
template<class T>
struct ptr_has_to<T, volatile void> {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<>
struct ptr_void<volatile void> {
BOOST_STATIC_CONSTEXPR bool value = true;
template<class T>
struct ptr_has_to<T, const volatile void> {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<>
struct ptr_void<const volatile void> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
template<class, class E, bool = ptr_void<E>::value>
template<class T, class E, bool = ptr_has_to<T, E>::value>
struct ptr_to { };
template<class T, class E>
struct ptr_to<T, E, false> {
struct ptr_to<T, E, true> {
static T pointer_to(E& v) {
return T::pointer_to(v);
}
};
template<class T>
struct ptr_to<T*, T, false> {
struct ptr_to<T*, T, true> {
static T* pointer_to(T& v) BOOST_NOEXCEPT {
return boost::addressof(v);
}

View File

@ -6,17 +6,15 @@ 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)
#if !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
#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;
char x, y;
};
template<class O>
@ -28,41 +26,40 @@ class has_pointer_to {
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;
static int* pointer_to(int& value) {
return &value;
}
};
struct P3 {
typedef void element_type;
};
struct P4 {
typedef int element_type;
};
struct P5 {
typedef int element_type;
static int* pointer_to() {
return 0;
}
};
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<P4> >::value);
BOOST_TEST(!has_pointer_to<boost::pointer_traits<P5> >::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();