我如何明智地重载放置操作符新? [英] How could I sensibly overload placement operator new?

查看:128
本文介绍了我如何明智地重载放置操作符新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++允许重载运算符新 - 全局和每类 - 通常运算符新 operator new [] 用于 new [] 语句和位置

C++ allows overloading operator new - both global and per-class - usual operator new, operator new[] used with new[] statement and placement operator new separately.

这三个中的前两个通常被重载使用自定义分配器和添加跟踪。但是放置 operator new 看起来很简单 - 它实际上什么都不做。例如,在Visual C ++中,默认实现只返回传入调用的地址:

The former two of those three are usually overloaded for using customized allocators and adding tracing. But placement operator new seems pretty straightforward - it actually does nothing inside. For example, in Visual C++ the default implementation just returns the address passed into the call:

//from new.h
inline void* operator new( size_t, void* where )
{
   return where;
}

它还能做什么?为什么和如何可以明智地重载放置运算符新

What else could it do? Why and how could I sensibly overload placement operator new?

推荐答案


§18.4。1.3放置表单

这些函数是保留的,C ++程序可能不定义替换标准C ++库中的版本的函数。

§18.4.​1.3 Placement forms
These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library.

:分配和释放操作符的唯一目的是分配和释放内存,所以当给定内存时,不应该做任何事情。 (该标准特别注意到这些功能故意不执行任何其他动作。)

The rationale: The only purpose of the allocation and deallocation operators is to allocate and deallocate memory, so when given memory nothing more should be done. (The standard specifically notes that these functions "Intentionally perform no other action.")

这篇关于我如何明智地重载放置操作符新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆