C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

partial specialization on std::vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sun Oct 29, 2006 10:11 am    Post subject: partial specialization on std::vector Reply with quote



i want to create a vector of pointer s.t. it can handle new and delete
but also have std::vector interface

can i implement by partial specialization and inherence like follow ?

#include <vector>
#include <algorithm>

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

template<typename T>
class auto_vector<T *> : public std::vector<T *> {
public:
typedef std::vector<T *> Base;
~auto_vector() {
clear();
}

void clear() {
std::for_each(begin(), end(), delete_ptr<T>());
Base::clear();
}

void push_back(const T *p) {
T* tmp = new T(*p)
Base::push_back(tmp);
}
};
Back to top
John Carson
Guest





PostPosted: Sun Oct 29, 2006 10:11 am    Post subject: Re: partial specialization on std::vector Reply with quote



<lokchan (AT) gmail (DOT) com> wrote in message
news:1162099118.599105.315010 (AT) b28g2000cwb (DOT) googlegroups.com
Quote:
i want to create a vector of pointer s.t. it can handle new and delete
but also have std::vector interface

can i implement by partial specialization and inherence like follow ?

#include <vector
#include <algorithm

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

template<typename T
class auto_vector<T *> : public std::vector<T *> {
public:
typedef std::vector<T *> Base;
~auto_vector() {
clear();
}

void clear() {
std::for_each(begin(), end(), delete_ptr<T>());
Base::clear();
}

void push_back(const T *p) {
T* tmp = new T(*p)
Base::push_back(tmp);
}
};

I suggest you might like to look at the Boost Pointer Containers:

http://www.boost.org/libs/ptr_container/doc/ptr_container.html


--
John Carson
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.