问题
In most places where the C++ standard library allocates memory, the user is able to customise this by providing a class which meets the Allocator requirements. For example, almost all containers take an allocator template argument, and std::allocate_shared returns a shared_ptr whose contained element and control block are both allocated via a provided Allocator.
However, there are a few places where the standard library can (potentially) allocate memory, but no Allocator support is provided. The ones I can think of are:
std::make_unique()(no correspondingallocate_unique())std::anystd::function(allocator support will be removed in C++17)std::valarraystd::basic_filebuf(althoughstd::basic_stringbufdoes use an Allocator)std::inplace_merge()
Questions:
- I'm sure this list is incomplete, but what else have I missed?
- Of the non-Allocator classes and functions, are they specified to use global
::operator new, plainnew, or is the memory source unspecified? - If anybody knows, what are the reasons for not providing allocator support in
any, and removing it fromfunction?
回答1:
Not an exhaustive list.
- Everything in
<stdexcept>, which needs to allocate memory to store the message string. - The standard pool/monotonic memory resource classes obviously allocate memory, but they do it from another memory resource, not an allocator, so they technically fit your description.
boyer_moore_searcherandboyer_moore_horspool_searcher.- Several
<algorithm>algorithms attempt to obtain additional memory (e.g.,stable_partition,stable_sort) and all parallel algorithms (regardless of header) may need additional memory. - Many things in the filesystem library:
path; that one used to use the default allocator internally, but looks like the newest draft removed that requirement, although it seems to be the intent still.directory_iteratorandrecursive_directory_iterator.- Some filesystem operations that can construct temporary
paths .
basic_regex.threadandasync.packaged_task, allocator support removed in C++17.promisewill need somewhere to put the shared state.- iostreams.
- Many things are hard-coded to use the default allocator:
error_code::message(),error_condition::message(),error_category::message(): these return astring, so default allocator only.bitset's stream insertion operator notionally callsto_stringwith the default allocator, though no high-quality implementation would do that in practice.- The
to_stringandto_wstringfree functions returnstd::string/std::wstringrespectively; no chance to specify your own allocator. Obviously the user-defined literals forstring(e.g.,"foo"s) also have no custom allocator support. - There are several facets in
<locale>that have member functions returning eitherstd::stringorstd::basic_string<charT>, i.e., using the default allocator (e.g.,numpunct), or accepting onlybasic_string<charT>(e.g.,money_get). - Various types in
<random>uses avectorwith the default allocator.
If anybody knows, what are the reasons for not providing allocator support in
any, and removing it fromfunction?
any's allocator support is unimplementable. function's allocator support is poorly specified and plagued with issues.
来源:https://stackoverflow.com/questions/43056338/standard-library-facilities-which-allocate-but-dont-use-an-allocator