mirror of
https://github.com/boostorg/core.git
synced 2025-07-30 04:47:24 +02:00
Support C++03 implementations that require reference and const_pointer
This commit is contained in:
@ -21,9 +21,8 @@ The header <boost/core/default_allocator.hpp> provides the class template
|
|||||||
|
|
||||||
* Like C++2a's `std::allocator`, does not provide members such as `construct()`
|
* Like C++2a's `std::allocator`, does not provide members such as `construct()`
|
||||||
and `destroy()` to be eligible for optimizations by allocator-aware code that
|
and `destroy()` to be eligible for optimizations by allocator-aware code that
|
||||||
detects the absence of these members to provide more optimal construction of
|
detects the absence of these members to provide more optimal construction.
|
||||||
objects.
|
* Supports `BOOST_NO_EXCEPTIONS` in allocation.
|
||||||
* Supports `BOOST_NO_EXCEPTIONS`.
|
|
||||||
* Does not have `std` as an associated namespace.
|
* Does not have `std` as an associated namespace.
|
||||||
|
|
||||||
[endsect]
|
[endsect]
|
||||||
@ -37,6 +36,9 @@ template<class T>
|
|||||||
struct default_allocator {
|
struct default_allocator {
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
typedef T* pointer;
|
typedef T* pointer;
|
||||||
|
typedef const T* const_pointer;
|
||||||
|
typedef std::add_lvalue_reference_t<T> reference;
|
||||||
|
typedef std::add_lvalue_reference_t<const T> const_reference;
|
||||||
typedef std::size_t size_type;
|
typedef std::size_t size_type;
|
||||||
typedef std::ptrdiff_t difference_type;
|
typedef std::ptrdiff_t difference_type;
|
||||||
typedef ``['true_type]`` propagate_on_container_move_assignment;
|
typedef ``['true_type]`` propagate_on_container_move_assignment;
|
||||||
|
@ -12,6 +12,12 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
#include <new>
|
#include <new>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
|
||||||
|
#if defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||||
|
#define BOOST_CORE_NO_CXX11_ALLOCATOR
|
||||||
|
#elif defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000
|
||||||
|
#define BOOST_CORE_NO_CXX11_ALLOCATOR
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
#if defined(BOOST_NO_EXCEPTIONS)
|
#if defined(BOOST_NO_EXCEPTIONS)
|
||||||
@ -35,10 +41,28 @@ struct true_type {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
struct add_reference {
|
||||||
|
typedef T& type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct add_reference<void> {
|
||||||
|
typedef void type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct add_reference<const void> {
|
||||||
|
typedef const void type;
|
||||||
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
struct default_allocator {
|
struct default_allocator {
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
typedef T* pointer;
|
typedef T* pointer;
|
||||||
|
typedef const T* const_pointer;
|
||||||
|
typedef typename add_reference<T>::type reference;
|
||||||
|
typedef typename add_reference<const T>::type const_reference;
|
||||||
typedef std::size_t size_type;
|
typedef std::size_t size_type;
|
||||||
typedef std::ptrdiff_t difference_type;
|
typedef std::ptrdiff_t difference_type;
|
||||||
typedef true_type propagate_on_container_move_assignment;
|
typedef true_type propagate_on_container_move_assignment;
|
||||||
@ -97,6 +121,18 @@ struct default_allocator {
|
|||||||
::operator delete(p, std::nothrow);
|
::operator delete(p, std::nothrow);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(BOOST_CORE_NO_CXX11_ALLOCATOR)
|
||||||
|
template<class U, class V>
|
||||||
|
void construct(U* p, const V& v) {
|
||||||
|
::new(p) U(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class U>
|
||||||
|
void destroy(U* p) {
|
||||||
|
p->~U();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T, class U>
|
template<class T, class U>
|
||||||
|
@ -7,6 +7,8 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
*/
|
*/
|
||||||
#include <boost/core/default_allocator.hpp>
|
#include <boost/core/default_allocator.hpp>
|
||||||
#include <boost/core/lightweight_test_trait.hpp>
|
#include <boost/core/lightweight_test_trait.hpp>
|
||||||
|
#include <vector>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
class type {
|
class type {
|
||||||
public:
|
public:
|
||||||
@ -44,6 +46,42 @@ void test_pointer()
|
|||||||
boost::default_allocator<void>::pointer);
|
boost::default_allocator<void>::pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_const_pointer()
|
||||||
|
{
|
||||||
|
BOOST_TEST_TRAIT_SAME(const int*,
|
||||||
|
boost::default_allocator<int>::const_pointer);
|
||||||
|
BOOST_TEST_TRAIT_SAME(const type*,
|
||||||
|
boost::default_allocator<type>::const_pointer);
|
||||||
|
BOOST_TEST_TRAIT_SAME(const int(*)[5],
|
||||||
|
boost::default_allocator<int[5]>::const_pointer);
|
||||||
|
BOOST_TEST_TRAIT_SAME(const void*,
|
||||||
|
boost::default_allocator<void>::const_pointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_reference()
|
||||||
|
{
|
||||||
|
BOOST_TEST_TRAIT_SAME(int&,
|
||||||
|
boost::default_allocator<int>::reference);
|
||||||
|
BOOST_TEST_TRAIT_SAME(type&,
|
||||||
|
boost::default_allocator<type>::reference);
|
||||||
|
BOOST_TEST_TRAIT_SAME(int(&)[5],
|
||||||
|
boost::default_allocator<int[5]>::reference);
|
||||||
|
BOOST_TEST_TRAIT_SAME(void,
|
||||||
|
boost::default_allocator<void>::reference);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_const_reference()
|
||||||
|
{
|
||||||
|
BOOST_TEST_TRAIT_SAME(const int&,
|
||||||
|
boost::default_allocator<int>::const_reference);
|
||||||
|
BOOST_TEST_TRAIT_SAME(const type&,
|
||||||
|
boost::default_allocator<type>::const_reference);
|
||||||
|
BOOST_TEST_TRAIT_SAME(const int(&)[5],
|
||||||
|
boost::default_allocator<int[5]>::const_reference);
|
||||||
|
BOOST_TEST_TRAIT_SAME(const void,
|
||||||
|
boost::default_allocator<void>::const_reference);
|
||||||
|
}
|
||||||
|
|
||||||
void test_size_type()
|
void test_size_type()
|
||||||
{
|
{
|
||||||
BOOST_TEST_TRAIT_SAME(std::size_t,
|
BOOST_TEST_TRAIT_SAME(std::size_t,
|
||||||
@ -203,10 +241,25 @@ void test_not_equals()
|
|||||||
boost::default_allocator<int>()));
|
boost::default_allocator<int>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_container()
|
||||||
|
{
|
||||||
|
std::vector<int, boost::default_allocator<int> > v;
|
||||||
|
v.push_back(1);
|
||||||
|
BOOST_TEST(v.size() == 1);
|
||||||
|
BOOST_TEST(v.front() == 1);
|
||||||
|
std::list<int, boost::default_allocator<int> > l;
|
||||||
|
l.push_back(1);
|
||||||
|
BOOST_TEST(l.size() == 1);
|
||||||
|
BOOST_TEST(l.front() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
test_value_type();
|
test_value_type();
|
||||||
test_pointer();
|
test_pointer();
|
||||||
|
test_const_pointer();
|
||||||
|
test_reference();
|
||||||
|
test_const_reference();
|
||||||
test_size_type();
|
test_size_type();
|
||||||
test_difference_type();
|
test_difference_type();
|
||||||
test_propagate_on_container_move_assignment();
|
test_propagate_on_container_move_assignment();
|
||||||
@ -219,5 +272,6 @@ int main()
|
|||||||
test_allocate_deallocate();
|
test_allocate_deallocate();
|
||||||
test_equals();
|
test_equals();
|
||||||
test_not_equals();
|
test_not_equals();
|
||||||
|
test_container();
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user