1
0
forked from boostorg/core

Use a different workaround for supporting noinit_adaptor in C++03

This commit is contained in:
Glen Fernandes
2022-03-15 11:58:38 -04:00
parent 4defdfd233
commit 5e0ff1680f
5 changed files with 72 additions and 68 deletions

View File

@ -430,30 +430,15 @@ allocator_allocate(A& a, typename allocator_size_type<A>::type n,
namespace detail {
#if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T, T>
struct alloc_no {
char x, y;
template<class A, class = void>
struct alloc_has_construct {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A, class T>
class alloc_has_construct {
template<class O>
static alloc_no<void(O::*)(T*), &O::construct>
check(int);
template<class O>
static alloc_no<void(O::*)(T*) const, &O::construct>
check(int);
template<class O>
static alloc_no<void(*)(T*), &O::construct>
check(int);
template<class>
static char check(long);
public:
static const bool value = sizeof(check<A>(0)) > 1;
template<class A>
struct alloc_has_construct<A,
typename alloc_void<typename A::_default_construct_destroy>::type> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
#else
template<class A, class T, class... Args>
@ -483,16 +468,14 @@ struct alloc_if<true, T> {
#if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class A, class T>
inline typename detail::alloc_if<detail::alloc_has_construct<A,
T>::value>::type
inline typename detail::alloc_if<detail::alloc_has_construct<A>::value>::type
allocator_construct(A& a, T* p)
{
a.construct(p);
}
template<class A, class T>
inline typename detail::alloc_if<!detail::alloc_has_construct<A,
T>::value>::type
inline typename detail::alloc_if<!detail::alloc_has_construct<A>::value>::type
allocator_construct(A&, T* p)
{
::new((void*)p) T();
@ -550,25 +533,15 @@ allocator_construct(A&, T* p, Args&&... args)
namespace detail {
#if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class A, class, class = void>
struct alloc_has_destroy {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A, class T>
class alloc_has_destroy {
template<class O>
static alloc_no<void(O::*)(T*), &O::destroy>
check(int);
template<class O>
static alloc_no<void(O::*)(T*) const, &O::destroy>
check(int);
template<class O>
static alloc_no<void(*)(T*), &O::destroy>
check(int);
template<class>
static char check(long);
public:
static const bool value = sizeof(check<A>(0)) > 1;
struct alloc_has_destroy<A, T,
typename alloc_void<typename A::_default_construct_destroy>::type> {
BOOST_STATIC_CONSTEXPR bool value = true;
};
#else
template<class A, class T>
@ -605,6 +578,11 @@ allocator_destroy(A&, T* p)
namespace detail {
#if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T, T>
struct alloc_no {
char x, y;
};
template<class A>
class alloc_has_max_size {
template<class O>

View File

@ -15,6 +15,8 @@ namespace boost {
template<class A>
struct noinit_adaptor
: A {
typedef void _default_construct_destroy;
template<class U>
struct rebind {
typedef noinit_adaptor<typename allocator_rebind<A, U>::type> other;

View File

@ -26,16 +26,6 @@ struct A2 {
};
#endif
template<class T>
struct A3 {
typedef T value_type;
A3() { }
template<class U>
void construct(U* p) {
::new((void*)p) U(1);
}
};
int main()
{
{
@ -52,11 +42,5 @@ int main()
BOOST_TEST_EQ(i, 6);
}
#endif
{
A3<int> a;
int i = 0;
boost::allocator_construct(a, &i);
BOOST_TEST_EQ(i, 1);
}
return boost::report_errors();
}

View File

@ -8,23 +8,59 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/allocator_access.hpp>
#include <boost/core/lightweight_test.hpp>
struct S {
static int count;
S() {
++count;
}
S(const S&) {
++count;
}
~S() {
--count;
}
};
int S::count = 0;
template<class T>
struct A {
struct A1 {
typedef T value_type;
A() { }
A1() { }
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
A2() { }
template<class U>
void destroy(U* p) {
*p = U();
}
};
#endif
int main()
{
A<int> a;
int i[3] = { 5, 5, 5 };
boost::allocator_destroy_n(a, &i[0], 3);
BOOST_TEST_EQ(i[0], 0);
BOOST_TEST_EQ(i[1], 0);
BOOST_TEST_EQ(i[2], 0);
{
A1<int> a;
S s[3];
boost::allocator_destroy_n(a, &s[0], 3);
BOOST_TEST_EQ(S::count, 0);
::new((void*)&s[0]) S();
::new((void*)&s[1]) S();
::new((void*)&s[2]) S();
}
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
{
A2<int> a;
int i[3] = { 5, 5, 5 };
boost::allocator_destroy_n(a, &i[0], 3);
BOOST_TEST_EQ(i[0], 0);
BOOST_TEST_EQ(i[1], 0);
BOOST_TEST_EQ(i[2], 0);
}
#endif
return boost::report_errors();
}

View File

@ -29,6 +29,7 @@ struct A1 {
A1() { }
};
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T>
struct A2 {
typedef T value_type;
@ -38,6 +39,7 @@ struct A2 {
*p = U();
}
};
#endif
int main()
{
@ -48,11 +50,13 @@ int main()
BOOST_TEST_EQ(S::count, 0);
::new((void*)&s) S();
}
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
{
A2<int> a;
int i = 5;
boost::allocator_destroy(a, &i);
BOOST_TEST_EQ(i, 0);
}
#endif
return boost::report_errors();
}