gpt4 book ai didi

c++ - `operator delete` 带大小参数和不带大小参数 : which one is chosen when both are available?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:32 25 4
gpt4 key购买 nike

当我在 GCC 和 Clang 中运行此代码示例时

struct S
{
int a;

void *operator new(size_t s)
{
std::cout << "new " << s << std::endl;
return ::operator new(s);
}

void operator delete(void *p, size_t s) noexcept
{
std::cout << "delete " << s << std::endl;
::operator delete(p);
}

void operator delete(void *p) noexcept
{
std::cout << "delete " << "none" << std::endl;
::operator delete(p);
}
};

int main()
{
S *p = new S;
delete p;
}

我从 GCC 和 Clang 得到以下输出

new 4
delete none

这意味着编译器选择了 operator delete 的“无大小”版本。

但是,如果我尝试使用全局替换的 operator newoperator delete 函数进行类似操作

struct S
{
int a;
};

void *operator new(size_t s)
{
std::cout << "new " << s << std::endl;
return std::malloc(s);
}

void operator delete(void *p, size_t s) noexcept
{
std::cout << "delete " << s << std::endl;
std::free(p);
}

void operator delete(void *p) noexcept
{
std::cout << "delete " << "none" << std::endl;
std::free(p);
}

int main()
{
S *p = new S;
delete p;
}

从 GCC 我得到

new 4
delete 4

我从 Clang 得到

new 4
delete none

我知道类内 operator delete 的“sized”版本自 C++98 以来一直存在于 C++ 中,但纵观 C++98,我似乎无法做到找到第一个示例中应选择哪个版本的 operator delete 问题的明确答案。甚至指定了吗?

第二个示例中的 C++14 及其“大小”版本的全局 operator delete 又如何呢?语言是否说明应该选择哪个版本?

最佳答案

这是 CWG issue 255 , 追溯到 2000 年。引用它的前提:

Paragraph 4 of 15.5 [class.free] speaks of looking up a deallocation function. While it is an error if a placement deallocation function alone is found by this lookup, there seems to be an assumption that a placement deallocation function and a usual deallocation function can both be declared in a given class scope without creating an ambiguity. The normal mechanism by which ambiguity is avoided when functions of the same name are declared in the same scope is overload resolution; however, there is no mention of overload resolution in the description of the lookup. In fact, there appears to be nothing in the current wording that handles this case. That is, the following example appears to be ill-formed, according to the current wording:

struct S {
void operator delete(void*);
void operator delete(void*, int);
};
void f(S* p) {
delete p; // ill-formed: ambiguous operator delete
}

该问题的状态目前为“正在起草”,在撰写此答复时似乎仍未解决。没有关于释放函数的重载决议的措辞。

Clang 和 GCC 似乎是在任意选择。我认为发出某种关于运算符模棱两可的诊断会更好。

关于c++ - `operator delete` 带大小参数和不带大小参数 : which one is chosen when both are available?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56368799/

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