forked from boostorg/core
Make pointer_traits SFINAE friendly
This commit is contained in:
@@ -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 ;
|
||||
|
75
test/pointer_traits_pointer_to_sfinae_test.cpp
Normal file
75
test/pointer_traits_pointer_to_sfinae_test.cpp
Normal 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
|
83
test/pointer_traits_rebind_sfinae_test.cpp
Normal file
83
test/pointer_traits_rebind_sfinae_test.cpp
Normal 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
|
101
test/pointer_traits_sfinae_test.cpp
Normal file
101
test/pointer_traits_sfinae_test.cpp
Normal 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();
|
||||
}
|
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user