【发布时间】:2014-07-29 15:23:22
【问题描述】:
我编写的以下代码必须调用放置解除分配和分配函数。
#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.
}
5.3.4/22 说 N3797:
放置释放函数的声明与 一个布局分配函数的声明,如果它有相同的 参数的数量,并且在参数转换 (8.3.5) 之后,所有 除了第一个参数类型是相同的。如果查找发现 单个匹配释放函数,该函数将被调用; 否则,不会调用释放函数。
C++11中没有实现还是我的理解有误?
【问题讨论】:
-
我以为只有在对象构建过程中出现异常时才会调用放置释放函数? Live example
-
@dyp 很好的例子!谢谢!我只是不知道 ecatmur 和 T.C. 引用的限制
标签: c++ c++11 memory-management