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 { namespace detail {
#if defined(BOOST_NO_CXX11_ALLOCATOR) #if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T, T> template<class A, class = void>
struct alloc_no { struct alloc_has_construct {
char x, y; BOOST_STATIC_CONSTEXPR bool value = false;
}; };
template<class A, class T> template<class A>
class alloc_has_construct { struct alloc_has_construct<A,
template<class O> typename alloc_void<typename A::_default_construct_destroy>::type> {
static alloc_no<void(O::*)(T*), &O::construct> BOOST_STATIC_CONSTEXPR bool value = true;
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;
}; };
#else #else
template<class A, class T, class... Args> template<class A, class T, class... Args>
@ -483,16 +468,14 @@ struct alloc_if<true, T> {
#if defined(BOOST_NO_CXX11_ALLOCATOR) #if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class A, class T> template<class A, class T>
inline typename detail::alloc_if<detail::alloc_has_construct<A, inline typename detail::alloc_if<detail::alloc_has_construct<A>::value>::type
T>::value>::type
allocator_construct(A& a, T* p) allocator_construct(A& a, T* p)
{ {
a.construct(p); a.construct(p);
} }
template<class A, class T> template<class A, class T>
inline typename detail::alloc_if<!detail::alloc_has_construct<A, inline typename detail::alloc_if<!detail::alloc_has_construct<A>::value>::type
T>::value>::type
allocator_construct(A&, T* p) allocator_construct(A&, T* p)
{ {
::new((void*)p) T(); ::new((void*)p) T();
@ -550,25 +533,15 @@ allocator_construct(A&, T* p, Args&&... args)
namespace detail { namespace detail {
#if defined(BOOST_NO_CXX11_ALLOCATOR) #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> template<class A, class T>
class alloc_has_destroy { struct alloc_has_destroy<A, T,
template<class O> typename alloc_void<typename A::_default_construct_destroy>::type> {
static alloc_no<void(O::*)(T*), &O::destroy> BOOST_STATIC_CONSTEXPR bool value = true;
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;
}; };
#else #else
template<class A, class T> template<class A, class T>
@ -605,6 +578,11 @@ allocator_destroy(A&, T* p)
namespace detail { namespace detail {
#if defined(BOOST_NO_CXX11_ALLOCATOR) #if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class T, T>
struct alloc_no {
char x, y;
};
template<class A> template<class A>
class alloc_has_max_size { class alloc_has_max_size {
template<class O> template<class O>

View File

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

View File

@ -26,16 +26,6 @@ struct A2 {
}; };
#endif #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() int main()
{ {
{ {
@ -52,11 +42,5 @@ int main()
BOOST_TEST_EQ(i, 6); BOOST_TEST_EQ(i, 6);
} }
#endif #endif
{
A3<int> a;
int i = 0;
boost::allocator_construct(a, &i);
BOOST_TEST_EQ(i, 1);
}
return boost::report_errors(); 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/allocator_access.hpp>
#include <boost/core/lightweight_test.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> template<class T>
struct A { struct A1 {
typedef T value_type; 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> template<class U>
void destroy(U* p) { void destroy(U* p) {
*p = U(); *p = U();
} }
}; };
#endif
int main() int main()
{ {
A<int> a; {
int i[3] = { 5, 5, 5 }; A1<int> a;
boost::allocator_destroy_n(a, &i[0], 3); S s[3];
BOOST_TEST_EQ(i[0], 0); boost::allocator_destroy_n(a, &s[0], 3);
BOOST_TEST_EQ(i[1], 0); BOOST_TEST_EQ(S::count, 0);
BOOST_TEST_EQ(i[2], 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(); return boost::report_errors();
} }

View File

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