allocator

Why are are std::allocator's construct and destroy functions deprecated in c++17?

南笙酒味 提交于 2019-11-28 17:04:38
问题 The c++17 specification deprecates the construct and destroy members of the std::allocator object. The working group provided rationale for deprecating other member functions here, under the heading "Deprecate the redundant members of std::allocator". However they don't mention specifically why those two members are deprecated or what the recommendation is for replacing that functionality. I'm assuming the implication is to use std::allocator_traits::construct instead. I'm a bit confused

Can I assume allocators don't hold their memory pool directly (and can therefore be copied)?

你。 提交于 2019-11-28 10:11:08
I'm writing a container and would like to permit the user to use custom allocators, but I can't tell if I should pass allocators around by reference or by value. Is it guaranteed (or at least, a reasonable assumption to make) that an allocator object will not contain its memory pool directly, and hence it would be OK to copy an allocator and expect the memory pools of the allocators to be cross-compatible? Or do I always need to pass allocators by reference? (I have found that passing by reference harms performance by a factor of > 2 because the compiler starts worrying about aliasing, so it

SSE and C++ containers

百般思念 提交于 2019-11-28 10:04:43
Is there an obvious reason why the following code segfaults ? #include <vector> #include <emmintrin.h> struct point { __m128i v; point() { v = _mm_setr_epi32(0, 0, 0, 0); } }; int main(int argc, char *argv[]) { std::vector<point> a(3); } Thanks Edit: I'm using g++ 4.5.0 on linux/i686, I might not know what I'm doing here, but since even the following segfaults int main(int argc, char *argv[]) { point *p = new point(); } I really think it must be and alignment issue. Ben Voigt The obvious thing that could have gone wrong would be if v wasn't aligned properly. But it's allocated dynamically by

Should C++ allocator::allocate throw or return nullptr when allocation fails?

。_饼干妹妹 提交于 2019-11-28 05:03:23
问题 The Allocator concept and std::allocator_traits do not say what allocate will do when allocation fail -- will it returns nullptr or throw? When I'm writing a container using standard allocator API, should I Check the return value and catch the exception in the noexcept version member function(E.g. push_back , resize ...); Check the return value and throw if fail in the exception-throwing one so that no matter it throws or not, I will get the correct behavior. 回答1: Draft n4659 for C++ standard

How to make my uninitialised_allocator safe?

南笙酒味 提交于 2019-11-28 04:50:51
问题 Following from this question, I want to use an unitialised_allocator with, say, std::vector to avoid default initialisation of elements upon construction (or resize() of the std::vector (see also here for a use case). My current design looks like this: // based on a design by Jared Hoberock template<typename T, typename base_allocator > struct uninitialised_allocator : base_allocator::template rebind<T>::other { // added by Walter Q: IS THIS THE CORRECT CONDITION? static_assert(std::is

How can I create a std::function with a custom allocator?

你。 提交于 2019-11-28 00:43:50
To save some code lets say I have a custom allocator named MyAlloc which I have successfully used with a std::vector<int> as follows: std::vector<int,MyAlloc<int>> vec; now I want to save a lambda in a std::function using the custom allocator, how do I do it? My failed attempt: int i[100]; std::function<void(int)> f(MyAlloc<void/*what to put here?*/>{},[i](int in){ //... }); Update: allocators in std::function have been depricated According to the standard, you need to give a tag type as the first argument to indicate that you want to use a custom allocator: std::function<void(int)> f(std:

std::align and std::aligned_storage for aligned allocation of memory blocks

孤街浪徒 提交于 2019-11-27 13:29:33
问题 I'm trying to allocate a block of memory of size size which needs to be Alignment aligned where the size may not be defined at compile time. I know routines such as _aligned_alloc , posix_memalign , _mm_alloc , etc exist but I do not want to use them as they bring down code portability. C++11 gives a routine std::align and also a class std::aligned_storage from which I can retrieve a POD type to allocate an element which will be aligned to my requirements. However my goal is to create an

polymorphic_allocator: when and why should I use it?

帅比萌擦擦* 提交于 2019-11-27 11:24:38
Here is the documentation on cppreference , here is the working draft. I must admit that I didn't understand what's the real purpose of polymorphic_allocator and when/why/how I should use it. As an example, the pmr::vector has the following signature: namespace pmr { template <class T> using vector = std::vector<T, polymorphic_allocator<T>>; } What does the polymorphic_allocator offer? What does the std::pmr::vector offer as well in regard of the old-fashioned std::vector ? What can I do now that I wasn't able to do till now? What's the real purpose of that allocator and when should I use it

How can I write a stateful allocator in C++11, given requirements on copy construction?

 ̄綄美尐妖づ 提交于 2019-11-27 04:13:25
问题 As far as I can tell, the requirements on an allocator to be used with STL containers are laid out in Table 28 of section 17.6.3.5 of the C++11 standard. I'm a bit confused about the interaction between some of these requirements. Given a type X that is an allocator for type T , a type Y that is "the corresponding allocator class" for type U , instances a , a1 , and a2 of X , and an instance b of Y , the table says: The expression a1 == a2 evaluates to true only if storage allocated from a1

How to I create a boost interprocess vector of interprocess containers?

余生长醉 提交于 2019-11-27 03:40:06
问题 I like to create a boost interprocess vector of classes containing a interprocess container. The following code works until the resize function call and of course because my class has not default constructor. How to I resolve this problem? The example is based on the boost Containers of containers example Thanks Markus #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost