Circumventing __USE_MALLOC and templatized containers
Wolfgang Bangerth
wolfgang.bangerth@iwr.uni-heidelberg.de
Wed Nov 14 12:15:00 GMT 2001
Hi there,
the suggested best way to make memory allocation for containers is no more
to use __USE_MALLOC, but to typedef them:
typedef std::string<char,some_allocator> my_string;
That is reasonable and simple since the number of character types for
which this has to be repeated is finite. However, it gets more difficult
if you want to do that for containers where the template arg is not
predictable. This, of course, works:
typedef std::vector<int,some_allocator> my_int_vector;
typedef std::vector<char,some_allocator> my_char_vector;
But that of course defeats the purpose of templates, and you can no more
write code like
template <typename T>
void do_something (T t) {
std::vector<T> v;
...
};
Since typedefs are not templatizable, the only way to avoid writing the
allocator everywhere the vector occurs seems to be to subclass from
std::vector:
template <typename T>
class my_vector : public std::vector<T,some_allocator>
{...};
This is a bad idea, obviously. What then is the suggested way to have
containers that are thread-safe without having to specify an allocator at
all places where they are used?
Best regards
Wolfgang
-------------------------------------------------------------------------
Wolfgang Bangerth email: wolfgang.bangerth@iwr.uni-heidelberg.de
www: http://gaia.iwr.uni-heidelberg.de/~wolf
More information about the Libstdc++
mailing list