Template placement of new and destructor

why doesn't it compile?

template <typename T> class Pool{ char Buff[sizeof(T)*256]; public: Pool(){ T* item = reinterpret_cast<T*>(&Buff[0]); for(int i =0 ; i<256;i++) item[i] = new(&item[i]) T(); } ~Pool(){ T* item = reinterpret_cast<T*>(&Buff[0]); for(int i =0 ; i<256;i++) item[i] -> ~ T(); } void reset(unsigned int i){ T* item = reinterpret_cast<T*>(&Buff[0]); item[i]->~T(); item[i]->T(); } } 

What I obviously want to achieve is calling a new place in an array of raw arrays (which should call the constructor in order). Then I want to call the destructor and constructor of the elements in the array. The problem is that Item is a template, so if I use

 Pool<FooBar> 

the compiler expects to find "FooBar ()" and "~ FooBar ()" instead of "T ()" and "~ T ()". is there any specific syntax for this?

I am using C ++ 03 not C ++ 11

+4
source share
1 answer

Your syntax is not quite right. The following should do the trick:

 Pool() { T* item = reinterpret_cast<T*>(&Buff[0]); for(int i = 0; i < 256; i++) new(&item[i]) T(); } ~Pool() { T* item = reinterpret_cast<T*>(&Buff[0]); for (int i = 0; i < 256; i++) item[i].~T(); } void reset(unsigned int i) { T* item = reinterpret_cast<T*>(&Buff[0]); item[i].~T(); new(&item[i]) T(); } 
+6
source

All Articles