gpt4 book ai didi

c++ - 为什么允许重新定义全局运算符 new(size_t)?

转载 作者:搜寻专家 更新时间:2023-10-31 00:08:32 26 4
gpt4 key购买 nike

为什么允许重新定义全局 operator new(size_t)

据我所知,在c++标准库中有一个相同的默认operator new(size_t)(学习自c++ Primer 5th)

default operator new在全局命名空间中,为什么user-defined global operator new与默认operator new不冲突 ?既然在同一个作用域内,难道不会出现重定义错误吗?

最佳答案

标准(语言支持库 -> 动态内存管理)对这些功能说:

[new.delete.single]

Replaceable: A C++ program may define functions with either of thesefunction signatures, and thereby displace the default versions definedby the C++ standard library.

[replacement.functions]

The program's definitions are used instead of the default versionssupplied by the implementation ([support.dynamic]). Such replacementoccurs prior to program startup ([basic.def.odr], [basic.start]). Theprogram's declarations shall not be specified as inline.

No diagnostic is required.

[defns.replacement]

Replacement function

non-reserved function whosedefinition is provided by a C++ program [ Note: Only one definitionfor such a function is in effect for the duration of the program'sexecution, as the result of creating the program ([lex.phases]) andresolving the definitions of all translation units ([basic.link]).— end note  ]

关于c++ - 为什么允许重新定义全局运算符 new(size_t)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48682413/

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