Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost unique_ptr Deletor

Tags:

c++

boost

If I want to create a unique_ptr of type QueueList (some self defined object), how do I define a deletor for it or is there already a template 'Deletor' I can use?

I want a unique_ptr so I can safely transfer the object between threads, without sharing it between the threads.

EDIT

boost::interprocess::unique_ptr<QueueList> LIST;  ///FAILS to COMPILE!!!

LIST mylist;

Compiler: MS Visual Studio 2003

ERROR:

error C2976: 'boost::interprocess::unique_ptr' : too few template arguments

error C2955: 'boost::interprocess::unique_ptr' : use of class template requires template argument list : see declaration of 'boost::interprocess::unique_ptr'

like image 262
Tony The Lion Avatar asked Oct 06 '10 14:10

Tony The Lion


1 Answers

Here is a simple deleter class that just calls delete on any given object:

template<typename T> struct Deleter {
    void operator()(T *p)
    {
        delete p;
    }
};

You can then use it with unique_ptr like this:

boost::interprocess::unique_ptr<QueueList, Deleter<QueueList> > LIST;
like image 96
Amanieu Avatar answered Nov 19 '22 21:11

Amanieu