GCC 4.2 Template strange error
i have the following code compiled with GCC 4.2 / XCode.
template <typename T>
class irrProcessBufferAllocator
{
public:
T* allocate(size_t cnt)
{
return allocProcessBufferOfType<T>(cnt);
}
void deallocate(T* ptr)
{
if (ptr)
{
releaseProcessBuffer(ptr);
}
}
void construct(T* ptr, const T& e)
{
new ((void*)ptr) T(e);//"error: expected type-specifier before 'e' " and
//error: expected `;' before 'e'
}
void destruct(T* ptr)
{
ptr->~T();//error: expected class-name before ';' token
}
};
i really can't figure o开发者_C百科ut how to fix the errors. please help,
Thanks.
Just to make sure, you are not missing necessary includes: <cstddef>
for std::size_t
and <new>
for placement new?
Otherwise those functions would appear to be correct. If that is the entire allocator, it has other flaws, such as missing required typedefs, address()
and max_size()
methods, as well as a rebind
template.
Edit: The only cause for the error could be that you have a function-style macro T defined.
#define T(z) zzz
would make the preprocessor replace all T()
's it encounters, but leave the T
s not followed by brackets.
You could just rename the template argument.
How about this?
template <class T>
class irrProcessBufferAllocator
{
public:
T* allocate(size_t cnt)
{
return allocProcessBufferOfType<T>(cnt);
}
void deallocate(T* ptr)
{
if (ptr)
{
releaseProcessBuffer(ptr);
}
}
void construct(T* ptr, const T& e)
{
new ((void*)ptr) T(e);//"error: expected type-specifier before 'e' " and
//error: expected `;' before 'e'
}
void destruct(T* ptr)
{
ptr->~T();//error: expected class-name before ';' token
}
};
int main(){
irrProcessBufferAllocator<int> i, j;
int *p = new int;
i.construct(p, 2);
i.destruct(p);
}
This just points out the problem , doesn't fix it. i have specialized the template for all POD types by removing the 'new' and '->~T()' from the construct and destruct functions. The errors still appear in the same spot.
精彩评论