allocator

Setting a custom allocator for strings

。_饼干妹妹 提交于 2019-12-04 08:35:42
问题 I know I can set a custom allocator for vectors using the syntax vector<T, Alloc> . Is there a way I can do the same for strings? 回答1: Yes. All string classes come from the class template basic_string , declared as such: template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_string; For example, std::string is just typedef basic_string<char> string; . The third template parameter is the allocator, so you can do something like: typedef basic

Does std::allocator handle over-aligned types in C++17?

北城以北 提交于 2019-12-04 08:11:38
C++17 introduces std::aligned_alloc and alignment-aware new that can do over-aligned allocations, but what about std::allocator ? Does it handle over-aligned types? In N4659(C++17 DIS), 23.10.9.1 [allocator.members], bullet 2 T* allocate(size_t n); Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T . Compared to C++14, the sentence It is implementation-defined whether over-aligned types are supported has been removed. So std::allocator should support over-aligned types in C++17. 来源: https://stackoverflow.com

Requirements on standard library allocator pointer types

前提是你 提交于 2019-12-04 03:49:57
I am trying to write a quadtree sparse matrix class. In short, a quadtree_matrix<T> is either the zero matrix or a quadruple (ne, nw, se, sw) of quadtree_matrix<T> . I'd like eventually to test different allocation schemes since this will probably impact the performance of linear algebra operations. So I will also template quadtree_matrix on a standard allocator type, so that I can reuse existing allocators. I will have to allocate two different kind of data: either a T , or a node , which contains four pointers (to either T or node). For all the algorithms I will consider, I know for sure

Converting between vectors with different allocators

怎甘沉沦 提交于 2019-12-03 17:45:24
问题 I have written a simple C++11 style stateful allocator type. Given template<typename T> class my_allocator { // the usual stuff }; template<typename T> using my_vector = std::vector<T, my_allocator<T>>; my_vector<int> x; std::vector<int> y = x; // error What is the best way to allow conversions from a my_vector to a std::vector using the default allocator? GCC 4.7 (recent svn) says error: conversion from 'my_vector<int> {aka std::vector<int, my_allocator<int>>}' to non-scalar type 'std:

Deprecation of std::allocator<void>

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:41:29
问题 Related: Why do standard containers require allocator_type::value_type to be the element type? It is said that the following has been deprecated since C++17: template<> struct allocator<void>; I wonder whether it is deprecated because the primary template alone is now able to accommodate allocator<void> , or the use-case of allocator<void> is deprecated. If latter, I wonder why. I think allocator<void> is useful in specifying an allocator not bound to a specific type (so just some schema

Why allow `propagate_on_container_swap == false` in Allocators, when it might cause undefined behaviour?

。_饼干妹妹 提交于 2019-12-03 17:03:14
问题 Note: Originally asked by Matt Mcnabb as a comment on Why can swapping standard library containers be problematic in C++11 (involving allocators)?. The Standard (N3797) says that if progagate_on_container_swap inside an Allocator is std::false_type it will yield undefined behaviour if the two allocators involved doesn't compare equal. Why would the Standard allow such construct when it seems more than dangerous? 23.2.1p9 General Container Requirements [container.requirements.general] If

How to use boost::pool library to create a custom memory allocator

只愿长相守 提交于 2019-12-03 10:03:29
问题 I am new to boost and I want to know how exactly the boost::pool libraries can help me in creating a custom memory allocator. And I have two vector of struct objects. First vector is of structure type A, while second vector is of structure type B. How can I reuse the memory allocated to the first vector to the second vector. 回答1: Boost Pool is a library that defines a few allocator types. Obviously, the focus of the library is to provide Pool Allocators. Pool Allocators shine when you

STL Container: Constructor's Allocator parameter and scoped allocators

微笑、不失礼 提交于 2019-12-03 06:51:10
问题 There is a template parameter for STL containers to chose a custom allocator. It took a while, but I think I understand how it works. Somehow it isn't really nice because the given allocator type isn't used directly but it is rebound to the allocator of another type. Finally I can work with it. After reading the API I recognized that there is also the possibility to give allocators as constructor parameter. But how do I know which kind of allocator the container uses, if it internally rebinds

Is Stephen Lavavej's Mallocator the same in C++11?

风格不统一 提交于 2019-12-03 06:38:43
8 years ago, Stephen Lavavej published this blog post containing a simple allocator implementation, named the "Mallocator". Since then we've transitioned to the era of C++11 (and soon C++17) ... does the new language features and rules affect the Mallocator at all, or is it still relevant as is? STL himself has an answer to this question in his STL Features and Implementation techniques talk at CppCon 2014 (Starting at 26'30). The slides are on github. I merged the content of slides 28 and 29 below: #include <stdlib.h> // size_t, malloc, free #include <new> // bad_alloc, bad_array_new_length

Converting between vectors with different allocators

时光毁灭记忆、已成空白 提交于 2019-12-03 06:27:27
I have written a simple C++11 style stateful allocator type. Given template<typename T> class my_allocator { // the usual stuff }; template<typename T> using my_vector = std::vector<T, my_allocator<T>>; my_vector<int> x; std::vector<int> y = x; // error What is the best way to allow conversions from a my_vector to a std::vector using the default allocator? GCC 4.7 (recent svn) says error: conversion from 'my_vector<int> {aka std::vector<int, my_allocator<int>>}' to non-scalar type 'std::vector<int>' requested Obviously this could be done with, say, a simple conversion function such as template