Home > Archive > VC Language > June 2005 > allocator question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
allocator question
|
|
| Mycroft Holmes 2005-06-02, 4:01 pm |
| Hi to all,
when passing an allocator to SEVERAL containers at the same time, can we
rely on the "rebind" mechanism assuming that each container is able to
select its own allocator?
for example: is the following ok?
template <class T, class allocator_t>
class MyContainer
{
std::vector<T, allocator_t> v_;
std::map<T, int, std::less<T>, allocator_t> m_;
};
or should we be verbose and deduce the correct allocators out of the
sub-containers:
template <class T, class allocator_t>
class MyContainer
{
typedef typename allocator_t::template rebind<T>::other rebind1_t;
std::vector<double, rebind1_t> v_;
typedef typename allocator_t::template rebind< std::pair<const T,
int>::other rebind2_t;
// etc...
};
of course version #1 and version #2 declare a class whose members have
different types (say, m_ is map<T,int, ... allocator<T> > and map<T,int, ...
allocator< std::pair<...> > > ), but apart from that, are they "functionally"
equivalent?
| |
| Doug Harrison [MVP] 2005-06-02, 4:01 pm |
| On Thu, 2 Jun 2005 14:44:45 +0200, Mycroft Holmes wrote:
> Hi to all,
>
> when passing an allocator to SEVERAL containers at the same time, can we
> rely on the "rebind" mechanism assuming that each container is able to
> select its own allocator?
> for example: is the following ok?
>
>
> template <class T, class allocator_t>
> class MyContainer
> {
> std::vector<T, allocator_t> v_;
> std::map<T, int, std::less<T>, allocator_t> m_;
> };
>
>
> or should we be verbose and deduce the correct allocators out of the
> sub-containers:
>
> template <class T, class allocator_t>
> class MyContainer
> {
> typedef typename allocator_t::template rebind<T>::other rebind1_t;
> std::vector<double, rebind1_t> v_;
>
> typedef typename allocator_t::template rebind< std::pair<const T,
> int>::other rebind2_t;
> // etc...
>
> };
>
>
> of course version #1 and version #2 declare a class whose members have
> different types (say, m_ is map<T,int, ... allocator<T> > and map<T,int, ...
> allocator< std::pair<...> > > ), but apart from that, are they "functionally"
> equivalent?
Clause 23.1/8 says ctors take an "Allocator& argument, an allocator whose
value type is the same as the container's value_type". Since you specify
that allocator type as a template parameter, you should use the rebind
approach unless you already know your allocator_t has the correct type for
the container.
--
Doug Harrison
Microsoft MVP - Visual C++
|
|
|
|
|