简体   繁体   English

shared_ptr删除程序问题

[英]shared_ptr deleter issue

I am trying to attach deleter function in shared_ptr as we want to pool my memory but getting an strange error. 我试图在shared_ptr附加删除器功能,因为我们要缓冲我的内存,但收到一个奇怪的错误。 Please help me to resolve this error: 请帮助我解决此错误:

template<std::size_t size> class Allocator{
public:
    template<typename T> void Release(T *ptr)
    {

    }
    template<typename T> void GetMemory(std::shared_ptr<T> &item)
    {
        T *ptr = new T();
        //my pain point is
        item =  std::shared_ptr<T>(ptr, 
        std::bind(&Allocator<size>::Release, this, std::placeholders::_1));
    }
};

The error is: 错误是:

prog.cpp: In instantiation of 'void GetMemory(std::share_ptr<T> &item)  unsigned int size = 10u]':
prog.cpp:316:15:   required from here
prog.cpp:226:19: error: no matching function for call to 'bind(<unresolved overloaded function type>, Pool<10u>*, const std::_Placeholder<1>&)'
          std::bind(&Pool<test>::Release, this, std::placeholders::_1)); 
template<std::size_t size> class Allocator{
public:
    template<typename T> void Release(T *ptr)
    {

    }
    template<typename T> void GetMemory(std::share_ptr<T> &item)
    {
        T *ptr = new T();
        //my pain point is
        item =  std::shared_ptr<T>(ptr, 
        std::bind(&Allocator<size>::Release, this, std::placeholders::_1));
    }
};

You can use a lambda instead: 您可以改用lambda:

item =  std::shared_ptr<T>(ptr, [&](auto x) { this->Release(x); });

But if you must use std::bind , you have to do this: 但是,如果必须使用std::bind ,则必须这样做:

item =  std::shared_ptr<T>(ptr, 
    std::bind(&Allocator<size>::template Release<T>, this, std::placeholders::_1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM