c++ - 分配但不使用分配器的标准库设施

标签 c++ memory-management c++-standard-library allocator

在大多数 C++ 标准库分配内存的地方,用户可以通过提供满足 Allocator requirements 的类来自定义它。 .例如,几乎所有容器都采用分配器模板参数,std::allocate_shared 返回一个 shared_ptr,其包含的元素和控制 block 均通过提供的分配器进行分配。

但是,有几个地方标准库可以(可能)分配内存,但不提供 Allocator 支持。我能想到的是:

  • std::make_unique()(没有对应的allocate_unique())
  • std::any
  • std::function(分配器支持将在 C++17 中移除)
  • std::valarray
  • std::basic_filebuf(尽管 std::basic_stringbuf 确实使用分配器)
  • std::inplace_merge()

问题:

  • 我确定此列表不完整,但我还遗漏了什么?
  • 在非分配器类和函数中,它们是指定使用全局 ::operator new、普通 new,还是未指定内存源?
  • 如果有人知道,不在 any 中提供分配器支持并将其从 function 中移除的原因是什么?

最佳答案

不是详尽的 list 。

  • <stdexcept> 中的所有内容,需要分配内存来存储消息字符串。
  • 标准池/单调内存资源类显然分配内存,但它们是从另一个内存资源而不是分配器执行的,因此它们在技术上符合您的描述。
  • boyer_moore_searcherboyer_moore_horspool_searcher .
  • 几个<algorithm>算法试图获得额外的内存(例如 stable_partitionstable_sort )并且所有并行算法(无论 header 如何)都可能需要额外的内存。
  • 文件系统库中的许多东西:
    • path ;那个曾经在内部使用默认分配器,但看起来最新的草案删除了该要求,尽管它似乎仍然是意图。
    • directory_iteratorrecursive_directory_iterator .
    • 一些可以构造临时文件系统的操作path .
  • basic_­regex .
  • threadasync .
  • packaged_task ,在 C++17 中删除了分配器支持。
  • promise将需要在某个地方放置共享状态。
  • iostreams。
  • 许多事情都被硬编码为使用默认分配器:
    • error_code::message() , error_condition::message() , error_category::message() :这些返回 string ,所以只有默认分配器。
    • bitset的流插入运算符名义上调用 to_string使用默认分配器,尽管在实践中没有高质量的实现可以做到这一点。
    • to_stringto_wstring免费功能返回std::string/std::wstring分别;没有机会指定您自己的分配器。显然 string 的用户定义文字(例如,"foo"s)也没有自定义分配器支持。
    • <locale> 中有几个方面具有返回 std::string 的成员函数或 std::basic_string<charT> ,即使用默认分配器(例如 numpunct ),或仅接受 basic_string<charT> (例如,money_get)。
    • 各种类型 <random>使用 vector使用默认分配器。

If anybody knows, what are the reasons for not providing allocator support in any, and removing it from function?

any的分配器支持是 unimplementable . function的分配器支持指定不当,plagued with issues .

关于c++ - 分配但不使用分配器的标准库设施,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43056338/

相关文章:

c++ - std::enable_if 和 std::is_arithmetic 作为模板参数的问题

c++ - 打印这个矩阵的 if 语句是什么

objective-c - Objective-C 中的内存管理和将指针设置为 nil

java - 类加载期间的内存分配

c++ - 我们可以对 std::array 使用传统的指针算法吗?

c++无效指针从空闲列表中出列

c++ - 检查无用包含文件的工具是什么?(c++)

java - 将 C++ OpenGL 帧缓冲区代码移植到 LWJGL - 我使用什么类?

c - 结构和链表内存分配valgrind错误

c++ - C++ 标准模板库中 std::sort 的空间复杂度是多少?