allocator

why does allocator in c++ need a copy constructor?

落花浮王杯 提交于 2019-12-08 15:31:45
问题 It is said here that it's because of exception specification. I do not understand it. Does this question have any relationship with exception specification? 回答1: After reading through the tutorial I was a little confused myself by the wording. But I believe it's as simple as this: the tutorial was explaining why the allocator's template header shows allocator(const allocator&) throw(); and template <class U> allocator(const allocator<U>&) throw(); even though the copy constructor is fairly

Allocator propagation policies in your new modern C++ containers

依然范特西╮ 提交于 2019-12-08 10:52:53
问题 What is the reason for having these traits in a container (https://en.cppreference.com/w/cpp/memory/allocator_traits) propagate_on_container_copy_assignment Alloc::propagate_on_container_copy_assignment if present, otherwise std::false_type propagate_on_container_move_assignment Alloc::propagate_on_container_move_assignment if present, otherwise std::false_type propagate_on_container_swap Alloc::propagate_on_container_swap if present, otherwise std::false_type is_always_equal(since C++17)

Adding an allocator to a C++ class template for shared memory object creation

岁酱吖の 提交于 2019-12-07 16:19:33
问题 In short, my question is: If you have class, MyClass<T> , how can you change the class definition to support cases where you have MyClass<T, Alloc> , similar to how, say, STL vector provides. I need this functionality to support an allocator for shared memory. Specifically, I am trying to implement a ring buffer in shared memory. Currently it has the following ctor: template<typename ItemType> SharedMemoryBuffer<ItemType>::SharedMemoryBuffer( unsigned long capacity, std::string name ) where

Why do standard containers require allocator_type::value_type to be the element type?

旧巷老猫 提交于 2019-12-07 12:14:00
问题 Related: Deprecation of std::allocator<void>. The following description about template parameter Allocator is found for both std::vector and std::list (emphasis mine): An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T . The last sentence does not make sense to me. If a specific value_type is required, couldn't it just

Copy stateful allocator: standard library allocator semantics and internal memory

谁都会走 提交于 2019-12-07 11:23:56
问题 I am writing a collection of allocators, with the intention that they're to be used in very high performance environments, so a little bit of restricted usage (mediated by the compiler, not runtime errors) is desirable. I've been reading into the C++11 semantics of stateful allocators and how they're expected to be used by conforming containers. I've pasted a simple allocator below which just contains a block of memory within the allocator object. In C++03, this was illegal. template

suggestions for improving an allocator algorithm implementation

折月煮酒 提交于 2019-12-07 04:03:52
问题 I have a Visual Studio 2008 C++ application where I'm using a custom allocator for standard containers such that their memory comes from a Memory Mapped File rather than the heap. This allocator is used for 4 different use cases: 104-byte fixed size structure std::vector< SomeType, MyAllocator< SomeType > > foo; 200-byte fixed size structure 304-byte fixed size structure n-byte strings std::basic_string< char, std::char_traits< char >, MyAllocator< char > > strn; I need to be able to allocate

Is a shared_ptr's deleter stored in memory allocated by the custom allocator?

自闭症网瘾萝莉.ら 提交于 2019-12-06 16:57:05
问题 Say I have a shared_ptr with a custom allocator and a custom deleter. I can't find anything in the standard that talks about where the deleter should be stored: it doesn't say that the custom allocator will be used for the deleter's memory, and it doesn't say that it won't be. Is this unspecified or am I just missing something? 回答1: util.smartptr.shared.const/9 in C++ 11: Effects: Constructs a shared_ptr object that owns the object p and the deleter d. The second and fourth constructors shall

How is allocator-aware container assignment implemented?

浪子不回头ぞ 提交于 2019-12-06 05:39:43
问题 For example, from std::deque::operator = in C++ Reference: (1) Copy Assignment (const std::deque &other) Replaces the contents with a copy of the contents of other. If std::allocator_traits::propagate_on_container_copy_assignment() is true, the target allocator is replaced by a copy of the source allocator. If the target and the source allocators do not compare equal, the target (*this) allocator is used to deallocate the memory, then other's allocator is used to allocate it before copying

Adding an allocator to a C++ class template for shared memory object creation

北城以北 提交于 2019-12-06 02:55:41
In short, my question is: If you have class, MyClass<T> , how can you change the class definition to support cases where you have MyClass<T, Alloc> , similar to how, say, STL vector provides. I need this functionality to support an allocator for shared memory. Specifically, I am trying to implement a ring buffer in shared memory. Currently it has the following ctor: template<typename ItemType> SharedMemoryBuffer<ItemType>::SharedMemoryBuffer( unsigned long capacity, std::string name ) where ItemType is the type of the data to be placed in each slot of the buffer. Now, this works splendid when

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

半城伤御伤魂 提交于 2019-12-06 02:42:09
问题 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? 回答1: 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