stella
stella

Reputation: 2596

Shared pointer with deleter

I tried to use the shared_ptr with deleter:

class A{ };

void (*foo)(){ };

int main(){
    std::shared_ptr<A> sp(new A, foo); //error: too many arguments to function call, 
                                       //expected 0, have 1
}

How to fix that?

Upvotes: 1

Views: 229

Answers (3)

Liyuan Liu
Liyuan Liu

Reputation: 172

you can consider that
http://www.cplusplus.com/reference/memory/shared_ptr/shared_ptr/

std::shared_ptr p4 (new int, std::default_delete());

std::shared_ptr p5 (new int, [](int* p){delete p;}, std::allocator());

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596307

Your custom deleter is not implemented correctly. Try this instead:

void foo(A* p)
{
    // do something, or not, it is up to you...
}

int main()
{
    std::shared_ptr<A> sp(new A, foo);
}

https://ideone.com/DHGpMy

Upvotes: 1

billz
billz

Reputation: 45410

Your deleter function should take A* as type, for example look at below Deleter implementation:

struct A{ };

void Deleter(A* p){ delete p; };

int main(){
    std::shared_ptr<A> sp(new A, Deleter); 
    return 0;
}

Also, you only declared function pointer foo, you need to implement it.

Upvotes: 1

Related Questions