Does std::vector satisfy the container requirements for Boost.Interprocess allocators?

偶尔善良 提交于 2019-12-05 04:07:20

In C++ 11 allocator rules have slightly changed, but I don't think it affects your question.

You probably want to know first what standard says about it. But you'd actually want to check whether your specific STL implementation conforms to the standard and doesn't contain bugs.

For the second part I'd strongly recommend going to sources of and just checking that, it's not that hard actually.

Also, you could write your tests to see if it's actually working properly:

  • Create custom allocator:
    • use some custom type as pointer, const pointer;
    • In construct(), destruct() count number of calls;
  • Create YourCustomType to be used with the allocator which also counts number of constructions/destructions.
  • Now, create std::vetor<YourCustomType, YourCustomAllocator<YourCustomType>> instance, insert some elements, clear the vector, destroy it and see if:
    • number of construct() destruct() calls is equal to number of constructions destructions of YourCustomType.
    • typeid(YourCustomAllocator::pointer) == typeid(std::vetor<YourCustomType, YourCustomAllocator<YourCustomType>>::pointer)

That's how you can be sure that all restrictions apply.

As for the first part of the question, here's an old C++ standard (not C++ 11).

1 There's no way (properly implemented) vector will take an allocator out of nowhere. It'll use whatever allocator you provide, and will use it for everything. As for operator==, it is implemented in boost's allocator and thus it's boost's problem to make operator== work as they require. Although I wasn't able to find confirmation in the standard.

2 Unless there's a bug, std::vector<T, YourAllocator>::pointer should be allocator's pointer. cppreference.com says that, and the standard says that, (look for "Template class vector"):

    typedef typename Allocator::pointer               pointer;
    typedef typename Allocator::const_pointer         const_pointer;

Although the same standard says this about allocators: Implementations of containers described in this International Standard are permitted to assume that their Allocator template parameter meets the following two additional requirements beyond those in Table 6.

--All instances of a given allocator type are required to be inter- changeable and always compare equal to each other.

--The typedef members pointer, const_pointer, size_type, and differ- ence_type are required to be T*, T const*, size_t, and ptrdiff_t, respectively.

So, actually standard doesn't allow using any pointer types, but my guess is that actual STL implementations will work.

3 Just check the std::vector<T>::clear() method implementation to see if allocator::destroy is called. Check std::vector<T>::resize() method's implementation to see if allocator::construct is used. I was not able to find requirement of calling destroy and construct in the standard.

I think the answer is no. Because in practice (in C++98) and in theory (C++11 standard), std::vector pointer cannot be something else than T*.

That is why boost::interprocess::vector<T> uses boost::container::vector<T, boost::interprocess::allocator<T>> (instead of std::vector<T, boost::interprocess::allocator<T>>).

I have no reputation to comment, so I have to answer.. If two allocators compare equal, they are interchangeable. Allocators of same type that compare unequal may, for example, be initialized with different (shared) memory. See Allocator:

a1 == a2 => returns true only if the storage allocated by the allocator a1 can be deallocated through a2. Establishes reflexive, symmetric, and transitive relationship. Does not throw exceptions.

So, if your ShmVector instances are created with ShmemAllocators which compare equal, you must be safe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!