【发布时间】:2021-04-03 07:23:06
【问题描述】:
与 STL 和 std::basic_string 不同,std::function 没有默认分配器。为什么?
C++11 中没有std::function 的默认分配器。
但是这篇文章(https://docs.microsoft.com/en-us/cpp/standard-library/allocators?view=msvc-160)持相反的观点,它说:
在 C++11 中,所有采用分配器类型参数的标准库类型和函数都支持最小分配器接口,包括
std::function、shared_ptr、allocate_shared()和basic_string。
template< class Alloc >
function( std::allocator_arg_t, const Alloc& alloc,
std::nullptr_t ) noexcept;
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
> class basic_string;
template<class T,
class Allocator = std::allocator<T>
> class vector;
【问题讨论】:
-
你问的构造函数不是在 C++17 中删除了吗?
-
可能是因为这些函数的非分配器版本会使用“默认”分配器?那么指定它的唯一原因是覆盖默认值?
-
@JohnZwinck 所以,即使在 C++11 中,
std::function也没有默认分配器。但是这篇文章持有相反的观点,它说:“在 C++11 中,所有标准库采用分配器类型参数的类型和函数支持最小分配器接口,包括 std::function、shared_ptr、allocate_shared() 和 basic_string。" -
那你的问题是什么?我们不知道微软为什么这么写。
-
@JohnZwinck Miscrosoft 说
std::function有一个默认分配器,但据我所知,没有。
标签: c++ c++11 stl std-function allocator