Category : allocator

I’m trying to design an allocator aware container from scratch. The std::allocator_traits class has a member method called select_on_container_copy_construction(..). I saw a question covering the reason for its existence. But, I couldn’t get where I should use this method. Here is a simplified custom container with some notes on it. /*** Libraries ***/ #include <memory> ..

Read more

I have a custom allocator template MyAllocator<T>. I am attempting to create a class template that essentially boils down to the following: template<typename T> class MyClass<T> { std::vector<T, MyAllocator<T>> &data; public: MyClass(std::vector<T, MyAllocator<T>> &data) : data(data) {} MyClass(std::vector<T> &data) : data(data) {} My intention is to avoid copying data when its memory allocation was dictated ..

Read more

According to cppref: std::allocator<T>::allocate_at_least Allocates count * sizeof(T) bytes of uninitialized storage, where count is an unspecified integer value not less than n, by calling ::operator new (an additional std::align_val_t argument might be provided), but it is unspecified when and how this function is called. Then, this function creates an array of type T[count] in ..

Read more

The following code snippet(see on godbolt) shows that big allocators won’t increase the memory footprint of STL containers, but big comparators will. Why is it the case? // compiled with x86-64 gcc 10.3, -std=c++17 #include <functional> #include <iostream> #include <memory> #include <set> struct MyLess : public std::less<int> { char dummy[1024]; }; struct MyAllocator : public ..

Read more

I’ve written up a template function that takes as input a std::vector and returns pair-wise combinations. Mathematically, given n objects, find all pairs. Function declaration in .hpp file: template <typename T> std::vector<std::pair<T, T>> pairwise_combination(const std::vector<T>& dataset); Implementation in .cpp file: template <typename T> std::vector<std::pair<T, T>> pairwise_combination(const std::vector<T>& dataset) { std::vector<std::pair<T, T>> combinations; for (long unsigned ..

Read more

There is an stateless memory pool allocator class: template<typename T> class pool_allocator { public: using value_type = T; using pointer = value_type *; /* Default constructor */ constexpr pool_allocator( void ) noexcept = default; /* Converting constructor used for rebinding */ template<typename U> constexpr pool_allocator( const pool_allocator<U> & ) noexcept {} [[nodiscard]] pointer allocate( size_t ..

Read more

Context So I have a parser which can parse many different types of objects. I parse one file per object type using a function that has this typing: template <typename T> std::vector<T *> parseFeaturesfromFile(std::string) And when creating an object World, I use this function like this (in the constructor of World): const std::vector<C_type *> c_types ..

Read more

I am reading some C++ code and specifically trying to understand a customised container. The container has following the template parameters: template<typename T, typename Alloc> class container{ … }; Where T is the type of data like float or int. The Alloc is the allocator and could be one of the standard libraries ones. Within ..

Read more