[英]C++ function template partial specialization
基本上,我實現了自己的內存分配函數Malloc()
,
void Malloc(size_t size);
現在,我想實現自己的New和NewArray函數,我聲明了這兩個函數,如下所示:
// template
template <class T>
T* New(void);
template <class T>
T* NewArray(unsigned int num);
實現是:
template <class T>
T* MemPool::New<T>()
{
return (T *)Malloc(sizeof(T));
}
template <class T>
T* MemPool::NewArray<T>(unsigned int num)
{
if(num < 0)
return NULL;
return (T*) Malloc(sizeof(T) * num);
}
但是編譯失敗了:
MP.cpp:482:20: error: function template partial specialization ‘New<T>’ is not allowed
T* MemPool::New<T>()
^
MP.cpp:488:41: error: function template partial specialization ‘NewArray<T>’ is not allowed
T* MemPool::NewArray<T>(unsigned int num)
您在這里還有一個<T>
:
template <class T>
T* MemPool::New<T>()
// ^^^
應該只是:
template <class T>
T* MemPool::New()
與NewArray
相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.