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

@@ -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();
}