1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <memory>
#include <type_traits>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
template < typename T, typename Alloc = std::allocator<T> > struct slist
{
struct node { T value ; node* next ; /* ... */ };
// allocator we would use to allocate nodes
using slist_allocator = typename Alloc::template rebind<node>::other ;
// 'pointer' to node (compatible with the allocator we would use to allocate nodes)
using pointer_to_node = typename slist_allocator::pointer ;
// an allocator that is compatible (yields compatible pointers etc.)
// with the allocator we would use to allocate nodes
using compatible_allocator = typename slist_allocator::template rebind<T>::other ;
// 'pointer' to T (compatible with the allocator we would use to allocate nodes)
using compatible_pointer_to_T = typename std::allocator_traits<compatible_allocator>::pointer ;
// 'pointer to T (compatible with Alloc)
using pointer = typename std::allocator_traits<Alloc>::pointer ;
// the IS (library-wide requirements)requires that the type of compatible_allocator must be Alloc
static_assert( std::is_same< Alloc, compatible_allocator >::value,
"allocator Alloc does not conform to the library-wide requirements for allocators" ) ;
// therefore:
static_assert( std::is_same< pointer, compatible_pointer_to_T >::value,
"allocator Alloc does not conform to the library-wide requirements for allocators" ) ;
// ...
};
namespace interprocess = boost::interprocess ;
using my_allocator_type = interprocess::allocator< int, interprocess::managed_shared_memory::segment_manager > ;
template struct slist< int, my_allocator_type > ;
int main()
{
using pointer = slist< int, my_allocator_type >::pointer ;
std::cout << "slist< int, my_allocator_type >::pointer is int* (pointer to int): "
<< std::boolalpha << std::is_same<pointer,int*>::value << '\n' ;
std::cout << "slist< int, my_allocator_type >::pointer is a (dumb) pointer: "
<< std::is_pointer<pointer>::value << '\n' ;
std::cout << "nevertheless, if we dereference it, we would get a reference to int: "
<< std::is_same< decltype( *std::declval<pointer>() ), int& >::value << '\n' ;
}
|