[英]Unlike STL and std::basic_string, why there is no default allocator for std::function
Unlike STL and std::basic_string
, there is no default allocator for std::function
.与 STL 和std::basic_string
不同, std::function
没有默认分配器。 Why?为什么?
There is no default allocator for std::function
in C++11. C++11 中没有std::function
的默认分配器。 But this article( https://docs.microsoft.com/en-us/cpp/standard-library/allocators?view=msvc-160 ) holds the opposite opinion, which says that:但是这篇文章( https://docs.microsoft.com/en-us/cpp/standard-library/allocators?view=msvc-160 )持有相反的观点,它说:
In C++11 all the Standard Library types and functions that take an allocator type parameter support the minimal allocator interface, including
std::function
,shared_ptr
,allocate_shared()
, andbasic_string
.在 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;
In C++11 and later, the most commonly used non-default constructor is this one:在 C++11 及以后,最常用的非默认构造函数是这个:
template<class F> function(F);
I'm going to ignore the others for this discussion because they are less interesting but the same information applies to them.我将忽略其他讨论,因为它们不太有趣,但相同的信息适用于它们。
In C++11 and C++14 it also has this, which was removed in C++20:在 C++11 和 C++14 中也有这个,它在 C++20 中被删除:
template<class F, class Alloc> function(std::allocator_arg_t, const Alloc&, F);
Your question is why it does not have this instead of the above constructor in C++11 and C++14:你的问题是为什么它在 C++11 和 C++14 中没有这个而不是上面的构造函数:
template<class F, class Alloc> function(F, const Alloc& = Alloc());
The answer is that there is no way to invoke such a constructor using the default argument, because function(f)
always calls the first constructor, and C++ does not have a way to specify the Alloc
template parameter.答案是没有办法使用默认参数调用这样的构造函数,因为function(f)
总是调用第一个构造函数,而 C++ 没有办法指定Alloc
模板参数。 For more on that limitation of C++, see here: Can the template parameters of a constructor be explicitly specified?有关 C++ 限制的更多信息,请参见此处: 能否显式指定构造函数的模板参数?
Many other types like std::vector are different because their allocator template parameter is on the overall class type, not merely on its constructor.许多其他类型(如 std::vector)是不同的,因为它们的分配器模板参数位于整体 class 类型上,而不仅仅是在其构造函数上。
I assume that you are aware that allocator support in std::function
was removed in C++17, so my answer will cover C++11 and C++14, with the caveat that: I assume that you are aware that allocator support in std::function
was removed in C++17, so my answer will cover C++11 and C++14, with the caveat that:
std::function
's allocator support was poorly specified and inconsistently implemented.std::function
的分配器支持指定不佳且实现不一致。 Some implementations [did] not provide overloads (6-10) at all, some provide[d] the overloads but ignore[d] the supplied allocator argument, and some provide[d] the overloads and use[d] the supplied allocator for construction but not when thestd::function
[was] reassigned.有些实现 [did] 根本不提供重载 (6-10),有些提供 [d] 重载但忽略 [d] 提供的分配器参数,有些提供 [d] 重载并使用 [d] 提供的分配器构造但不是当std::function
[被]重新分配时。
In C++11 and C++14, std::function
did not have an allocator template parameter because it was not needed.在 C++11 和 C++14 中, std::function
没有分配器模板参数,因为它不需要。 All the members that would invoke an allocator also accepted an allocator as a function (constructor) template argument;所有将调用分配器的成员也接受分配器作为 function(构造函数)模板参数; and since std::function
performs type erasure on the wrapped callable object, also performing type erasure on an allocator would be straightforward.并且由于std::function
对包装的可调用 object 执行类型擦除,因此在分配器上执行类型擦除也很简单。
In the Microsoft documentation (my emphasis):在 Microsoft 文档中(我的重点):
In C++11 all the Standard Library types and functions that take an allocator type parameter support the minimal allocator interface, including
std::function
, [...]在 C++11 中,所有采用分配器类型参数的标准库类型和函数都支持最小分配器接口,包括std::function
[...]
This is referring to the constructors of std::function
taking allocator arguments (removed in C++17), which are functions taking an allocator;这是指std::function
的构造函数采用分配器 arguments(在 C++17 中删除),它们是采用分配器的函数; a constructor is a function in this context.在这种情况下,构造函数是 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.