forked from boostorg/core
Make pointer_to SFINAE friendlier
This commit is contained in:
@ -106,43 +106,74 @@ struct ptr_rebind<T, U,
|
|||||||
typedef typename T::template rebind<U> type;
|
typedef typename T::template rebind<U> type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class>
|
#if !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
|
||||||
struct ptr_void {
|
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;
|
BOOST_STATIC_CONSTEXPR bool value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<class T>
|
||||||
struct ptr_void<void> {
|
struct ptr_has_to<T, const void> {
|
||||||
BOOST_STATIC_CONSTEXPR bool value = true;
|
BOOST_STATIC_CONSTEXPR bool value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<class T>
|
||||||
struct ptr_void<const void> {
|
struct ptr_has_to<T, volatile void> {
|
||||||
BOOST_STATIC_CONSTEXPR bool value = true;
|
BOOST_STATIC_CONSTEXPR bool value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<class T>
|
||||||
struct ptr_void<volatile void> {
|
struct ptr_has_to<T, const volatile void> {
|
||||||
BOOST_STATIC_CONSTEXPR bool value = true;
|
BOOST_STATIC_CONSTEXPR bool value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<class T, class E, bool = ptr_has_to<T, E>::value>
|
||||||
struct ptr_void<const volatile void> {
|
|
||||||
BOOST_STATIC_CONSTEXPR bool value = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class, class E, bool = ptr_void<E>::value>
|
|
||||||
struct ptr_to { };
|
struct ptr_to { };
|
||||||
|
|
||||||
template<class T, class E>
|
template<class T, class E>
|
||||||
struct ptr_to<T, E, false> {
|
struct ptr_to<T, E, true> {
|
||||||
static T pointer_to(E& v) {
|
static T pointer_to(E& v) {
|
||||||
return T::pointer_to(v);
|
return T::pointer_to(v);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
struct ptr_to<T*, T, false> {
|
struct ptr_to<T*, T, true> {
|
||||||
static T* pointer_to(T& v) BOOST_NOEXCEPT {
|
static T* pointer_to(T& v) BOOST_NOEXCEPT {
|
||||||
return boost::addressof(v);
|
return boost::addressof(v);
|
||||||
}
|
}
|
||||||
|
@ -6,17 +6,15 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
(http://www.boost.org/LICENSE_1_0.txt)
|
(http://www.boost.org/LICENSE_1_0.txt)
|
||||||
*/
|
*/
|
||||||
#include <boost/config.hpp>
|
#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/pointer_traits.hpp>
|
||||||
#include <boost/core/lightweight_test.hpp>
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
|
||||||
#if !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class has_pointer_to {
|
class has_pointer_to {
|
||||||
template<class>
|
template<class>
|
||||||
struct result {
|
struct result {
|
||||||
char one;
|
char x, y;
|
||||||
char two;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class O>
|
template<class O>
|
||||||
@ -28,41 +26,40 @@ class has_pointer_to {
|
|||||||
public:
|
public:
|
||||||
static const bool value = sizeof(check<T>(0)) != 1;
|
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 P1 { };
|
||||||
|
|
||||||
struct P2 {
|
struct P2 {
|
||||||
typedef int element_type;
|
typedef int element_type;
|
||||||
|
|
||||||
|
static int* pointer_to(int& value) {
|
||||||
|
return &value;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct P3 {
|
struct P3 {
|
||||||
typedef void element_type;
|
typedef void element_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct P4 {
|
||||||
|
typedef int element_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct P5 {
|
||||||
|
typedef int element_type;
|
||||||
|
|
||||||
|
static int* pointer_to() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
BOOST_TEST((!has_pointer_to<boost::pointer_traits<P1> >::value));
|
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<P2> >::value);
|
||||||
BOOST_TEST(!has_pointer_to<boost::pointer_traits<P3> >::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<int*> >::value);
|
||||||
BOOST_TEST(!has_pointer_to<boost::pointer_traits<void*> >::value);
|
BOOST_TEST(!has_pointer_to<boost::pointer_traits<void*> >::value);
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
|
Reference in New Issue
Block a user