我写的以下代码必须调用放置删除和分配函数。
#include <iostream>
using namespace std;
struct A
{
void * operator new [] (size_t t, int, int)
{
cout << "3 parameters allocation" << endl;
return ::operator new[](t);
}
void operator delete [] (void *p, int, int)
{
cout << "3 parameters deallocation" << endl;
return ::operator delete[](p);
}
};
int main()
{
A *a = new (5,5) A[10]; //operator new [] (size_t, int, int) invoked
delete [] a; //Error. Overload resolution is failed.
}
根据 N3797 版本 5.3.4/22 规定:
如果一个底层释放函数的声明与已有的底层分配函数的声明相匹配,那么这个底层释放函数的声明必须和对应的底层分配函数的声明一致。通过转换参数(8.3.5),除去第一个参数外,所有参数类型必须都相同。如果查找到一个匹配的释放函数,则调用该函数;否则,不会调用任何释放函数。
这在 C++11
中是否实现了?还是我理解有误?