我正在尝试了解 boost::factory 并专门将其与自定义分配器一起使用
#include <boost/smart_ptr.hpp>
#include <boost/functional/factory.hpp>
#include <boost/pool/pool_alloc.hpp>
class Foo{
public:
Foo() {};
Foo(const std::string param) : m_param(param)
const std::string param() const { return m_param; }
private:
const std::string m_param;
};
int main(int argc,char** argv) {
// This doesn't work
boost::factory<Foo*,boost::pool_allocator<Foo*>> g;
// But this does
boost::factor<boost::shared_ptr<Foo>,boost::pool_allocator<Foo>> h;
auto x = h("http://www.bar.com");
// Test to call a method
x->param();
// Release memory - do I need to do that for shared_ptr?
boost::singleton_pool<boost::pool_allocator_tag,sizeof(Download)>::release_memory();
return 0;
}
我的问题 1. 为什么第一个版本不起作用?2. 如果shared_ptr还在做,为什么我需要释放分配器的内存?还是分配器只是用于构造,而销毁由shared_ptr处理?只是试图理解语义和“最佳实践”