在大多数 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_searcher
和boyer_moore_horspool_searcher
. - 几个
<algorithm>
算法试图获得额外的内存(例如stable_partition
、stable_sort
)并且所有并行算法(无论 header 如何)都可能需要额外的内存。 - 文件系统库中的许多东西:
-
path
;那个曾经在内部使用默认分配器,但看起来最新的草案删除了该要求,尽管它似乎仍然是意图。 -
directory_iterator
和recursive_directory_iterator
. - 一些可以构造临时文件系统的操作
path
.
-
-
basic_regex
. -
thread
和async
. -
packaged_task
,在 C++17 中删除了分配器支持。 -
promise
将需要在某个地方放置共享状态。 - iostreams。
- 许多事情都被硬编码为使用默认分配器:
-
error_code::message()
,error_condition::message()
,error_category::message()
:这些返回string
,所以只有默认分配器。 -
bitset
的流插入运算符名义上调用to_string
使用默认分配器,尽管在实践中没有高质量的实现可以做到这一点。 to_string
和to_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 fromfunction
?
any
的分配器支持是 unimplementable . function
的分配器支持指定不当,plagued with issues .
关于c++ - 分配但不使用分配器的标准库设施,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43056338/