2

私は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 によって処理されますか? セマンティクスと「ベストプラクティス」を理解しようとしているだけです

4

0 に答える 0