gpt4 book ai didi

c++ - g++ 4.9.0 允许我们使用 operator delete[](void*, size_t) 进行布局分配

转载 作者:搜寻专家 更新时间:2023-10-31 01:03:56 25 4
gpt4 key购买 nike

我正在使用 g++ 4.9.0 和 N3797 工作草案。我编写了以下简单示例:

#include <iostream>
#include <cstdlib>

using namespace std;

struct A
{
void* operator new[](size_t t, size_t)
{
cout << "allocation" << endl;
return ::operator new[](t);
}

void operator delete[](void *p, size_t t)
{
cout << "deallocation" << endl;
:: operator delete[](p);
}
};

int main()
{
A *a = new (90) A[5];
delete [] a;
}

demo

示例必须反射(reflect)第 3.7.4.2/2 节中的以下脚注规则:

The global operator delete[] with exactly two parameters, the second of which has type std::size_t, is a usual deallocation function.37

37) This deallocation function precludes use of an allocation function void operator new(std::size_t, std::size_t) as a placement allocation function

但事实并非如此。这是错误还是脚注只是对实现者的建议?

最佳答案

这是非法的,编译器应该发出诊断信息。来自 §5.3.4 [expr.new]/p22,重点添加:

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. If the lookup finds the two-parameter form of a usual deallocation function (3.7.4.2) and that function, considered as a placement deallocation function, would have been selected as a match for the allocation function, the program is ill-formed. For a non-placement allocation function, the normal deallocation function lookup is used to find the matching deallocation function (5.3.5) [ Example:

struct S {
// Placement allocation function:
static void* operator new(std::size_t, std::size_t);
// Usual (non-placement) deallocation function:
static void operator delete(void*, std::size_t);
};
S* p = new (0) S; // ill-formed: non-placement deallocation function matches
// placement allocation function

end example ]

铿锵 generates您的代码错误。

但是,请注意,与全局operator deleteoperator delete[] 不同,双参数operator deleteoperator delete[ ] 作为类成员的不一定是通常的释放函数(§3.7.4.2 [basic.stc.dynamic.deallocation]/p2):

If a class T has a member deallocation function named operator delete with exactly one parameter, then that function is a usual deallocation function. If class T does not declare such an operator delete but does declare a member deallocation function named operator delete with exactly two parameters, the second of which has type std::size_t, then this function is a usual deallocation function. Similarly, if a class T has a member deallocation function named operator delete[] with exactly one parameter, then that function is a usual (non-placement) deallocation function. If class T does not declare such an operator delete[] but does declare a member deallocation function named operator delete[] with exactly two parameters, the second of which has type std::size_t, then this function is a usual deallocation function.

因此,与全局释放函数不同,如果您声明成员 operator delete[] (void *),则 operator delete[] (void *, std::size_t) 不再是通常的释放函数,而是放置释放函数:

struct A
{
void* operator new[](size_t t, size_t)
{
cout << "allocation" << endl;
return ::operator new[](t);
}

void operator delete[](void *p)
{
cout << "deallocation - usual" << endl;
:: operator delete[](p);
}
void operator delete[](void *p, size_t t)
{
cout << "deallocation - placement" << endl;
:: operator delete[](p);
}
};

clang no longer报告此代码的错误。

关于c++ - g++ 4.9.0 允许我们使用 operator delete[](void*, size_t) 进行布局分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com