是否可以保证C ++标准库容器调用可替换的新函数? [英] Is it guaranteed that C++ standard library containers call the replaceable new functions?

查看:98
本文介绍了是否可以保证C ++标准库容器调用可替换的新函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我替换了所有的operator new签名,至少在经过测试的实现上,我会看到标准容器会调用替换后的版本来分配内存.

If I replace all the operator new signatures that I can, at least on the implementations I have tested, I see that the standard containers call into my replaced versions to allocate memory.

这是标准所保证的吗?也就是说,对于实现而言,使用未对标准容器底层的内存调用我的替换函数的优化版本是非法的吗?

Is this guaranteed by the standard? That is, would it be illegal for an implementation to use an optimized version which didn't call my replacement functions for the memory underlying the standard containers?

推荐答案

支持分配器的容器(例如std::vector<T>)的默认分配器是std::allocator<T>.该类模板在标准的[default.allocator]部分中进行了描述.根据C ++ 14中的[allocator.members]/6:

The default allocator for allocator-aware containers such as std::vector<T> is std::allocator<T>. This class template is described in section [default.allocator] of the standard. According to [allocator.members]/6 in C++14:

通过调用::operator new(std::size_t)

因此,全局运算符new是您需要替换的运算符.如果您专门为T重载了operator new,则默认分配器将使用该重载.

So the global operator new is the one that you need to replace. If you overloaded operator new specifically for T, that overload will not be used by the default allocator.

这篇关于是否可以保证C ++标准库容器调用可替换的新函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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