分配但不使用分配器的标准库设施
Standard library facilities which allocate but don't use an Allocator
在大多数 C++ 标准库分配内存的地方,用户可以通过提供满足 Allocator
requirements 的 class 来自定义它。例如,几乎所有容器都采用分配器模板参数,并且 std::allocate_shared
returns 一个 shared_ptr
其包含的元素和控制块都通过提供的分配器进行分配。
然而,有几个地方标准库可以(可能)分配内存,但不提供 Allocator
支持。我能想到的是:
std::make_unique()
(没有对应的allocate_unique()
)
std::any
std::function
(分配器支持将在 C++17 中删除)
std::valarray
std::basic_filebuf
(尽管 std::basic_stringbuf
使用分配器)
std::inplace_merge()
问题:
- 我确定此列表不完整,但我还遗漏了什么?
- 在非分配器 classes 和函数中,它们是指定使用全局
::operator new
、普通 new
,还是未指定内存源?
- 如果有人知道,在
any
中不提供分配器支持并将其从 function
中删除的原因是什么?
并非详尽列表。
<stdexcept>
中的所有内容,需要分配内存来存储消息字符串。
- 标准 pool/monotonic 内存资源 类 显然分配内存,但它们是从另一个内存资源而不是分配器执行的,因此它们在技术上符合您的描述。
boyer_moore_searcher
和 boyer_moore_horspool_searcher
.
- 几个
<algorithm>
算法试图获得额外的内存(例如,stable_partition
、stable_sort
)并且所有并行算法(不管 header)可能需要额外的内存。
- 文件系统库中的许多东西:
path
;那个曾经在内部使用默认分配器,但看起来最新的草案删除了该要求,尽管它似乎仍然是意图。
directory_iterator
和 recursive_directory_iterator
。
- 一些可以构造临时
path
s . 的文件系统操作
basic_regex
.
thread
和 async
.
packaged_task
,在 C++17 中删除了分配器支持。
promise
需要在某处放置共享状态。
- iostreams.
- 许多事情 hard-coded 使用默认分配器:
error_code::message()
、error_condition::message()
、error_category::message()
:这些 return 一个 string
,所以只有默认分配器。
bitset
的流插入运算符名义上使用默认分配器调用 to_string
,但实际上没有 high-quality 实现会这样做。
-
to_string
和 to_wstring
自由函数分别为 return std::string
/std::wstring
;没有机会指定您自己的分配器。显然 string
的 user-defined 文字(例如 "foo"s
)也没有自定义分配器支持。
<locale>
中有几个方面具有成员函数 returning 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
's allocator support is poorly specified and plagued with issues。
在大多数 C++ 标准库分配内存的地方,用户可以通过提供满足 Allocator
requirements 的 class 来自定义它。例如,几乎所有容器都采用分配器模板参数,并且 std::allocate_shared
returns 一个 shared_ptr
其包含的元素和控制块都通过提供的分配器进行分配。
然而,有几个地方标准库可以(可能)分配内存,但不提供 Allocator
支持。我能想到的是:
std::make_unique()
(没有对应的allocate_unique()
)std::any
std::function
(分配器支持将在 C++17 中删除)std::valarray
std::basic_filebuf
(尽管std::basic_stringbuf
使用分配器)std::inplace_merge()
问题:
- 我确定此列表不完整,但我还遗漏了什么?
- 在非分配器 classes 和函数中,它们是指定使用全局
::operator new
、普通new
,还是未指定内存源? - 如果有人知道,在
any
中不提供分配器支持并将其从function
中删除的原因是什么?
并非详尽列表。
<stdexcept>
中的所有内容,需要分配内存来存储消息字符串。- 标准 pool/monotonic 内存资源 类 显然分配内存,但它们是从另一个内存资源而不是分配器执行的,因此它们在技术上符合您的描述。
boyer_moore_searcher
和boyer_moore_horspool_searcher
.- 几个
<algorithm>
算法试图获得额外的内存(例如,stable_partition
、stable_sort
)并且所有并行算法(不管 header)可能需要额外的内存。 - 文件系统库中的许多东西:
path
;那个曾经在内部使用默认分配器,但看起来最新的草案删除了该要求,尽管它似乎仍然是意图。directory_iterator
和recursive_directory_iterator
。- 一些可以构造临时
path
s . 的文件系统操作
basic_regex
.thread
和async
.packaged_task
,在 C++17 中删除了分配器支持。promise
需要在某处放置共享状态。- iostreams.
- 许多事情 hard-coded 使用默认分配器:
error_code::message()
、error_condition::message()
、error_category::message()
:这些 return 一个string
,所以只有默认分配器。bitset
的流插入运算符名义上使用默认分配器调用to_string
,但实际上没有 high-quality 实现会这样做。-
to_string
和to_wstring
自由函数分别为 returnstd::string
/std::wstring
;没有机会指定您自己的分配器。显然string
的 user-defined 文字(例如"foo"s
)也没有自定义分配器支持。 <locale>
中有几个方面具有成员函数 returningstd::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
's allocator support is poorly specified and plagued with issues。