放置取消分配功能未调用 [英] Placement deallocation function is not called
问题描述
我编写的以下代码必须调用放置释放和分配功能.
The following code I write must invoke both placement deallocation and allocation functions.
#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:
5.3.4/22 says N3797:
展示位置释放功能的声明与 展示位置分配函数的声明(如果具有的话) 参数数量,并在参数转换后(8.3.5),全部 除第一个参数类型外,其他参数类型相同.如果查找发现一个 单个匹配的释放函数,将调用该函数; 否则,将不会调用释放函数.
A declaration of a placement deallocation function matches the declaration of a placement allocation function if it has the same number of parameters and, after parameter transformations (8.3.5), all parameter types except the first are identical. If the lookup finds a single matching deallocation function, that function will be called; otherwise, no deallocation function will be called.
它不是C++11
中的工具,还是我的误会?
It is not implement in C++11
or it is my misunderstanding?
推荐答案
如果提供了特定于类的释放函数,则没有由范围解析运算符作为前缀的 delete-expression 是错误的-除非具有一个或两个参数的特定于类的释放函数可用,否则将形成:
If a class-specific deallocation function is provided, a delete-expression that is not prefixed by the scope resolution operator is ill-formed unless a class-specific deallocation function with one or two parameters is available:
10-如果类型是完整的,并且如果释放函数查找同时找到仅具有指针参数的常规释放函数和具有指针参数和size参数的常规释放函数,则所选释放函数应为一个有两个参数.否则,所选解除分配函数应为具有一个参数的函数.
因此您需要:
- 提供
void operator delete[](void*);
, - 提供
void operator delete[](void*, size_t);
,或 - 写
::delete[] a
.
- provide
void operator delete[](void*);
, - provide
void operator delete[](void*, size_t);
, or - write
::delete[] a
.
这篇关于放置取消分配功能未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!