C++:无法使用scoped_allocator_adaptor传播polymorphic_allocator
C++: Can't propagate polymorphic_allocator with scoped_allocator_adaptor
我有一个vector<vector<int>>
,希望整个内存(即外部和内部向量)都取自memory_resource
。这里有一个简单的例子,首先是无聊的部分:
#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);
}
这给了我一个冗长的编译器警告,本质上是说它找不到内部向量的构造函数。
如果我使用的不是多态分配器,而是常规分配器(例如std::allocater-请参阅注释掉的行),那么一切似乎都能正常工作。
gcc错误消息比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<...>&
)
'
为什么boost会尝试通过两次传递分配器来构造向量?
此外,这里有一个使用STL(实验)而不是boost的版本。这条消息给出了一个实际的错误消息"如果uses_allocater为true,则必须有可能构造分配器",但这对我也没有帮助。
也许我理解了一些概念上的错误。这是解决问题的方法吗?还是有更好的方法来解决最初的问题?
Argh。解释隐藏在std::experimental::pmr::polymorphic_allocator::construct
:中
此函数由任何分配程序感知对象,例如std::vector,它被赋予std::polymorphic_allocator作为要使用的分配器。自从memory_resource*隐式转换为polymorphic_allocator内存资源指针将传播到任何有分配器意识的使用多态分配器的子对象。
所以多态分配器会自动传播。这也解释了为什么分配器在gcc错误消息中被传递了两次。
这是一个工作版本:
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);
}
这是我几个小时前需要的信息:
如何将polymorphic_allocater和scoped_allocater_aptor一起使用
你没有。确保所有内部容器也使用多态分配器,然后内存资源将自动移交。
- std::set<Key,Compare,Allocator>::find() 函数使用"<"运算符而不是"=="运算符背后的直觉是什么?
- 未定义模板"std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator&
- 我的类中应该有一个Allocator作为成员变量吗
- 返回的向量定义为 "std::vector<<error-type>, std::allocator<<error-type>>>
- basic_string allocator
- boost::adaptor::filters core dumps with boost::range_detail::d efault_constructible_unary_fn_wrapper
- 向量(容器)需要使用"allocator"吗?
- 没有从"std::allocator"到"const allocator_type"的转换
- 如果对象是可构造/可析构的,STL容器是否允许跳过调用allocator::construct和allocator::d
- 容器优化:为什么STL容器方法参数不再使用allocator::const_reference typedef
- 为什么std::allocator这么快?
- 为什么Allocator::reference被逐步淘汰
- 自定义Allocator编译困难
- 自定义Allocator编译困难
- 自定义Allocator编译困难
- Std::list抱怨缺少第二个模板参数(allocator)
- 为什么许多Allocator函数是可选的
- 解析allocator::rebind调用
- STL容器库-在allocator类的不同实例上调用allocate/deallocate是否合法?
- 为什么有两个std::allocator::construct函数?