Hacker News new | past | comments | ask | show | jobs | submit login

Ultimately this is why Halpern, Lakos, et al. pushed for polymorphic allocators to be added to C++. We make heavy use of them because it gives the caller the full control over the allocator used in nearly all situations then the code is written properly to take and propagate allocators according to the rules.

(A little example posted by Bartlomiej Filipek showing a vector neatly using a stack buffer)

  #include <iostream>
  #include <memory_resource>
  #include <vector>

  int main() {
      char buffer[64] = {};
      std::fill_n(std::begin(buffer), std::size(buffer)-1, '_');
      std::cout << buffer << '\n';
  
      std::pmr::monotonic_buffer_resource pool{
          std::data(buffer), std::size(buffer)
      };
  
      std::pmr::vector<char> vec{&pool};    
      for (char ch='a'; ch <= 'z'; ++ch)
          vec.push_back(ch);
  
      std::cout << buffer << '\n';
  }



I’m sorry, but I’m not seeing the point you’re trying to make from your example: std::vector takes a custom allocator as a template parameter already (also, the order of characters in implementation-specific in C++, FWIW).




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: