为什么下面的代码会给我们编译错误:
#include <iostream>
using namespace std;
int i = 0;
struct A
{
void * operator new [] (size_t t)
{
cout << "allocation" << endl;
return ::operator new[](t);
}
template <class T>
void operator delete [] (void *p, size_t)
{
cout << "deallocation" << endl;
return ::operator delete[](p);
}
};
int main()
{
A *a = new A[10];
delete [] a;//No suitable deallocation function
}
我认为 function template
只是一个与适当模板相匹配的函数族。这意味着必须在结构定义中找到非放置函数。
最佳答案
您的运算符 delete[]
采用 void*
,但有一个模板参数 T
,它未用作函数签名,因此编译器无法解析函数调用。
你想要:
void operator delete [] (void *p, size_t);
关于c++ - 函数模板不是普通函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25021854/