This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]