gpt4 book ai didi

c++ - 错误 "non-placement deallocation function"是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 20:30:57 27 4
gpt4 key购买 nike

编译 serna-free 包时出现以下错误:

build/buildd-serna-free_4.3.0.20110221-2-i386-pAsDoD/serna-free-4.3.0.20110221/
sfworks/common/RefCntStorage.h:76:10:
error: non-placement deallocation function 'static void
StringPrivate::RefCntData<E>::operator
delete(void*,StringPrivate::size_type) [with E = QChar,
StringPrivate::size_type = unsigned int]'
/build/buildd-serna-free_4.3.0.20110221-2-i386-pAsDoD/serna-free-4.3.0.20110221/
sfworks/common/RefCntStorage.h:135:9:
error: selected for placement delete

代码如下:

void operator delete(void* p, size_type)
{
::operator delete(p);
}

最佳答案

认为问题来自规范中的这个措辞:

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 (18.1), then this function is a usual deallocation function

这意味着如果您尝试声明一对 operator newoperator delete 并将 size_t 作为第二个参数,则编译器会认为您的 operator delete 具有以下签名:

void operator delete (void* memory, size_t arg)

是标准(非放置)释放器,而不是应该匹配 operator new(void*, size_t) 的放置释放器。

奇怪的是,规范中没有任何地方说这会导致编译器错误。事实上,规范只是说,如果你有这个错误,那么如果自定义 new 抛出异常,那么内存就不会被清理。如果有人知道为什么 g++ 将此报告为错误,我很想知道为什么(尤其是 如果我错了并且这确实应该是非法的)。

编辑:啊!问题似乎来自 C++0x。根据新标准的最新草案,§3.5.4/20:

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.

它特别列出了这样做会导致破损的示例。这是 C++0x 中的新功能这一事实可以解释为什么该错误仅在最新版本的 g++ 中出现。

关于c++ - 错误 "non-placement deallocation function"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5367674/

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