This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Circumventing __USE_MALLOC and templatized containers
- From: Wolfgang Bangerth <wolfgang dot bangerth at iwr dot uni-heidelberg dot de>
- To: libstdc++ at gcc dot gnu dot org
- Date: Fri, 23 Nov 2001 18:06:49 +0100 (MET)
- Subject: Circumventing __USE_MALLOC and templatized containers
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