In STL classes supporting an allocator, why are the allocator arguments not template templates?

All class templates that support STL allocators must be created using the allocator type. Wouldn't it be much more user-friendly if the allocator was not a template argument but a template template argument ?

To demonstrate, the std :: vector and std :: basic_string class templates have the following signatures, respectively:

template<class T, class Allocator = std::allocator<T>> class vector;
template<class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>> class basic_string;

      

If I have my own allocator:

template <typename T>
class MyAllocator
{
    // ...
};

      

and want to instantiate a vector of strings that my custom allocator uses to both allocate internal memory for the vector and internal character arrays, things quickly get awkward:

typedef std::vector<std::basic_string<char, std::char_traits<char>, MyAllocator<char> >, MyAllocator<std::basic_string<char, std::char_traits<char>, MyAllocator<char>>>> CustomAllocStringVector;

      

By using an additional typedef, this can be simplified somewhat:

typedef std::basic_string<char, std::char_traits<char>, MyAllocator<char>> CustomAllocString;
typedef std::vector<CustomAllocString, MyAllocator<CustomAllocString>> CustomAllocStringVector;

      

But my concern is why force the user to explicitly specify the full allocator type? If I use an allocator for a vector char , is it not worth saying that the allocator will be of type < char >?

If the signatures are std :: vector and std :: basic_string:

template<typename T, template <typename ElementType> class AllocatorType = std::allocator> class vector;
template<typename CharT, typename Traits = std::char_traits<CharT>, template <typename ElementType> class AllocatorType = std::allocator> class basic_string;

      

the same vector type as above can be more simple typedef'd like:

typedef std::basic_string<char, std::char_traits<char>, MyAllocator> CustomAllocString;
typedef std::vector<CustomAllocString, MyAllocator> CustomAllocStringVector;

      

My way, of course, would require all allocators to be templates, but shouldn't any allocator class, which should be as few bits as possible, meet this requirement?

I'm sure there is a good reason for this, but I don't see it at the moment.

+3


source to share


1 answer


This will result in a requirement that the type of the allocator be a class template with one template argument, specialized with a container value_type

. Your proposal will eliminate

template<typename T, unsigned int PoolNumber = 0>
class my_allocator;

      

as a valid distributor.



At the same time, I can just use typedef

what I already have for my allocator type and don't need to split it or repeat its template name:

template<typename T> class my_allocator;

typedef my_allocator<int> int_allocator;

std::list<int, int_allocator> ...    // valid currently, difficult to express with your proposal

      

+2


source







All Articles