C ++:无法使用scoped_allocator_adaptor传播polymorphic_allocator [英] C++: Can't propagate polymorphic_allocator with scoped_allocator_adaptor
问题描述
我有一个 vector< vector< int> int>
,并且希望从 memory_resource
中获取整个内存(即外部和内部矢量的全部)>.这是一个精简的示例,首先是无聊的部分:
I have a vector<vector<int>>
and want the entire memory (i.e., of both the outer and the inner vector) to be taken from a memory_resource
. Here is a stripped down example, first the boring part:
#include <boost/container/pmr/memory_resource.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <boost/container/pmr/polymorphic_allocator.hpp>
#include <iostream>
#include <string>
#include <vector>
// Sample memory resource that prints debug information
class MemoryResource : public boost::container::pmr::memory_resource {
void* do_allocate(std::size_t bytes, std::size_t alignment) {
std::cout << "Allocate " << bytes << " bytes" << std::endl;
return malloc(bytes);
}
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) { free(p); }
bool do_is_equal(const memory_resource& other) const noexcept { return true; }
};
这是我感兴趣的部分:
template <typename T>
using Alloc = boost::container::pmr::polymorphic_allocator<T>;
// using Alloc = std::allocator<T>;
template <typename T>
using PmrVector = std::vector<T, boost::container::scoped_allocator_adaptor<Alloc<T>>>;
using Inner = PmrVector<int>;
int main() {
MemoryResource resource{};
PmrVector<Inner> v(1000, Alloc<Inner>{&resource});
// PmrVector<Inner> v(1337, Alloc<Inner>{});
v[0].resize(100);
}
这给了我一个冗长的编译器警告,本质上说它找不到用于以下内容的构造函数内部向量.
This gives me a lengthy compiler warning, essentially saying that it can't find a constructor for the inner vector.
如果我不是使用多态分配器,而是使用常规分配器(例如std :: allocator-参见注释掉的行),那么一切似乎都可以正常工作.
If, instead of the polymorphic allocator, I use a regular allocator (e.g., std::allocator - see the lines that are commented out), everything seems to work.
gcc错误消息比clang更好:
The gcc error message is a bit better than that of clang:
/usr/local/include/boost/container/allocator_traits.hpp:415:10:
error: no matching function for call to '
std::vector<int, polymorphic_allocator<int> >::vector(
scoped_allocator_adaptor<...>&, polymorphic_allocator<...>&
)
'
为什么会通过两次分配器来增强向量的构造?
Why would boost try to construct a vector by passing the allocator twice?
此外,此处是使用STL(实验性)而不是boost的版本.给出了一个实际的错误消息如果uses_allocator为true,则必须使用分配器进行构造",但这也无济于事.
Also, here is a version that uses STL (experimental) instead of boost. That one gives an actual error message "construction with an allocator must be possible if uses_allocator is true", but that doesn't help me either.
也许我在理解概念上的错误.是这样做的方法还是有解决原始问题的更好方法?
Maybe I am understanding something conceptually wrong. Is this the way to do it or is there a better way to solve the original problem?
推荐答案
Argh.解释隐藏在 std :: experimental :: pmr :: polymorphic_allocator中::construct
:
Argh. The explanation is hidden in std::experimental::pmr::polymorphic_allocator::construct
:
任何人都可以通过std :: allocator_traits调用此函数分配器感知对象,例如std :: vector,被赋予了std :: polymorphic_allocator作为要使用的分配器.自从memory_resource *隐式转换为polymorphic_allocator,内存资源指针将传播到任何支持分配器的对象多态分配器的子对象.
This function is called (through std::allocator_traits) by any allocator-aware object, such as std::vector, that was given a std::polymorphic_allocator as the allocator to use. Since memory_resource* implicitly converts to polymorphic_allocator, the memory resource pointer will propagate to any allocator-aware subobjects using polymorphic allocators.
因此,事实证明,多态分配器会自动传播.这也解释了为什么分配器在gcc错误消息中被两次传递.
So it turns out that polymorphic allocators automatically propagate. That also explains why the allocator is passed twice in the gcc error message.
这是一个有效的版本:
template <typename T>
using Alloc = std::experimental::pmr::polymorphic_allocator<T>;
template <typename T>
using PmrVector = std::vector<T, Alloc<T>>;
using Inner = PmrVector<int>;
int main() {
MemoryResource resource{};
PmrVector<Inner> v(1000, Alloc<Inner>{&resource});
v[0].resize(100);
}
这是几个小时前我需要的信息:
And here is the information that I would have need a couple of hours ago:
我如何一起使用polymorphic_allocator和scoped_allocator_adaptor?
你不知道.确保所有内部容器也使用多态分配器,然后内存资源将被自动传递.
You don't. Make sure that all inner containers also use polymorphic allocators, then the memory resource will be handed down automatically.
这篇关于C ++:无法使用scoped_allocator_adaptor传播polymorphic_allocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!