Boost logo

Boost :

Subject: Re: [boost] Minimizing Dependencies within Generic Classes for Faster and Smaller Programs
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-11-12 13:33:35


----- Original Message -----
From: "Daniel James" <dnljms_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, November 12, 2010 5:36 PM
Subject: Re: [boost] Minimizing Dependencies within Generic Classes for Faster and Smaller Programs

On 12 November 2010 12:42, vicente.botet <vicente.botet_at_[hidden]> wrote:
>
> The iterator can depend on the Allocator::pointer but this doesn't forcerly means that it depends on the Allocator. If Allocator::pointer is a typedef to a type idependent of Allocator the independency is ensured.
>
> In addition we could make iterator depend on Allocator::void_pointer and use pointer_traits<>::rebind.
>
> Am I missing something?

If the node contains a pointer to node, then to work out the type of
the pointer, you need to know the type of the node. But you can't know
that until you know the type of the pointer. For example:

    template <typename NodePtr>
    struct node
    {
        NodePtr next_;
    };

How do you write the node typedef? What's the ??? parameter in:

    typedef typename node<
        typename Allocator::template rebind<???>::other::pointer
> node_type;
_______________________________
Vicente> has the following sens?

template <class T, class VoidPtr>
struct c_node {
    typedef typename pointer_traits<VoidPtr>::template
        rebind<c_node<T, VoidPtr> >::other node_pointer;
    node_pointer next_;
    T value_;
    c_node() : next_(static_cast<node_pointer>(this)) {}
};

____________________________________________________________
But if you use allocator:

    template <typename Allocator>
    struct node
    {
        typedef typename
            Allocator::template rebind<node>::other::pointer
            node_ptr;
        node_ptr next_;
   };

   typedef node<Allocator> node_type;

IIRC C++0x is going to introduce a trait for this purpose, so we'll be
able to write something like:

    template <typename VoidPtr>
    struct node
    {
        typedef typename
            convert_pointer<VoidPtr, node> node_ptr;
        node_ptr next_;
    };

But we can't do that until the trait is available for current custom
pointer types.

Daniel
_______________________________________________

Vicente>
You are right, we need this trait.
BTW, are you referring to pointer_traits when you use convert_pointer, isn't it?

Yes. One of the first things to do should be to define this trait class :(

With pointer_traits, the container iterator could look like:

template <class T, class VoidPtr>
class c_iterator {
    typedef typename pointer_traits<VoidPtr>::template
        rebind<c_node<T, VoidPtr> >::other node_pointer;
    node_pointer ptr_;
    explicit c_iterator(node_pointer p) : ptr_(p) {}
...
};

and the container implementation could look like

template <class T, class A>
class c_imp {
potected:
    typedef T value_type;
    typedef A allocator_type;
    typedef allocator_traits<allocator_type> alloc_traits;
    typedef typename alloc_traits::void_pointer void_pointer;
    typedef c_iterator<value_type, void_pointer> iterator;
...
};

In this way the c_iterator class doesn't depend on the Allocator. It depends only on the stored type T and the Allocator void_pointer type.

Best,
Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk