简体   繁体   English

与 STL 和 std::basic_string 不同,为什么 std::function 没有默认分配器

[英]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() , and basic_string .在 C++11 中,所有采用分配器类型参数的标准库类型和函数都支持最小分配器接口,包括std::functionshared_ptrallocate_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 the std::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.

相关问题 Stl容器的std :: basic_string类型 - Stl container of std::basic_string type 在分配器的特殊情况下std :: basic_string中的错误 - Bug in std::basic_string in special case of allocator ostream和运算符std :: basic_string <char, std::char_traits<char> ,std :: allocator <char> &gt;? - ostream and operator std::basic_string<char, std::char_traits<char>, std::allocator<char>>? 引用std :: basic_string使用自定义分配器作为std :: string的常量对象的字符串的特化而没有开销? - Reference std::basic_string specialization of string with custom allocator as a constant object of std::string without overhead? std :: basic_string的使用 - Uses of std::basic_string std::basic_string 特化 - std::basic_string specialization 未定义对`librarymanager :: getBook(std :: basic_string的引用 <char, std::char_traits<char> ,std :: allocator <char> &gt;) - undefined reference to `librarymanager::getBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) 1 IntelliSense:没有合适的构造函数可以从“bool”转换为“std :: basic_string” <char, std::char_traits<char> ,std :: allocator <char> &gt;” - 1 IntelliSense: no suitable constructor exists to convert from “bool” to “std::basic_string<char, std::char_traits<char>, std::allocator<char>>” 未定义对`readVector(std :: __ cxx11 :: basic_string的引用) <char, std::char_traits<char> ,std :: allocator <char> &gt;)” - undefined reference to `readVector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' main.cpp || 未定义对`MyClass :: loadDatas(std :: basic_string的引用 <char, std::char_traits<char> ,std :: allocator <char> &gt;)&#39;| - main.cpp|| undefined reference to `MyClass::loadDatas(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM