简体   繁体   English

关于STL中的不相等分配器

[英]Regarding non-equal allocators in STL

I am reading Item 10 in Effective STL by Scott Meyers on allocators in C++. 我正在阅读Scott Meyers在C ++中关于分配器的有效STL中的第10项。

Standard says that an implementation of the STL is permitted to assume that all allocator objects of the same type are equivalent and always compare equal. 标准说,允许STL的实现假定所有相同类型的分配器对象都是等效的,并且始终比较相等。

That's all well and good, but the more you think about it. 一切都很好,但是您会考虑的更多。 the more you'll realize just how draconian a restriction it is that STL implementations may assume that allocators of the same type are equivalent. 您越会意识到STL实现可能假定相同类型的分配器是等效的,这是多么严格的限制。 It means that portable allocator objects — allocators that will function correctly under different STL implementations — may not have state. 这意味着可移植的分配器对象(可以在不同的STL实现下正常运行的分配器)可能没有状态。 Let's be explicit about this: it means that portable allocators may not have any nonstatic data members, at least not any that affect their behavior. 让我们对此进行明确说明:这意味着可移植分配器可能没有任何非静态数据成员,至少没有任何影响其行为的成员。 None. 没有。 Nada. 纳达。 That means, for example, you can't have one SpecialAllocator that allocates from one heap and a different SpecialAllocator that allocates from a different heap. 举例来说,这意味着您不能有一个从一个堆分配的SpecialAllocator和另一个从另一个堆分配的SpecialAllocator。 Such allocators wouldn't be equivalent, and STL implementations exist where attempts to use both allocators could lead to corrupt runtime data structures. 这样的分配器将不等效,并且存在尝试使用这两个分配器可能导致运行时数据结构损坏的STL实现。

In fairness to the Standardization Committee, I should point out that it included the following statement immediately after the text that permits STL implementers to assume that allocators of the same type are equivalent: 公平地说,我要指出的是,在允许STL实现者假定相同类型的分配器等效的文本之后,它立即包含以下声明:

Implementors are encouraged to supply libraries that ... support non-equal instances. 鼓励实现者提供支持非相等实例的库。 In such implementations. 在这样的实现中。 ... the semantics of containers and algorithms when allocator instances compare non-equal are implementation-defined. ...当分配器实例比较不相等时,容器和算法的语义是实现定义的。

This is a lovely sentiment, but as a user of the STL who is considering the development of a custom allocator with state, it offers you next to nothing. 这是一种可爱的感觉,但是作为STL的用户,考虑开发具有状态的自定义分配器时,它几乎为您提供任何帮助。 You can take advantage of this statement only if (1) you know that the STL implementations you are using support inequivalent allocators, (2) you are willing to delve into their documentation to determine whether the implementation-defined behavior of "non-equal" allocators is acceptable to you, and 仅当(1)您知道所使用的STL实现支持不等价分配器,(2)您愿意深入研究其文档以确定实现定义的行为是否为“非相等”行为时,才可以利用此语句。分配器是您可以接受的,并且

(3) you're not concerned about porting your code to STL implementations that may take advantage of the latitude expressly extended to them by the Standard. (3)您不必担心将代码移植到STL实现中,这些实现可能会利用标准明确规定的扩展范围。 In short, this paragraph — paragraph 5 of section 20.1.5. 简而言之,本款-第20.1.5节的第5款。 for those who insist on knowing — is the Standard's "1 have a dream" speech for allocators. 对于那些坚持知道的人来说,这是标准的分配者的“ 1拥有梦想”的演讲。 Until that dream becomes common reality, programmers concerned about portability will limit themselves to custom allocators with no state. 在这个梦想成为现实之前,关注可移植性的程序员将自己局限于无状态的自定义分配器。

My question on above paragraph are 我对以上段落的问题是

  1. What does author mean by inequivalent or non-equal allocators? 作者是指不相等或不相等的分配器?

  2. What does last paragraph in above text ie, point 3 mean in simple terms? 以上文字的最后一段,即第3点,用简单的术语表示什么?

That information is out of date. 该信息已过期。 C++11 and later versions support stateful allocators. C ++ 11和更高版本支持状态分配器。

The quotes you have posted from Effective C++ are only of concern if you are writing a C++ library which requires custom allocators, does not require C++11, and which supports building against unknown/unspecified standard libraries. 只有在编写需要自定义分配器,不需要C ++ 11并支持针对未知/未指定标准库进行构建的C ++库时,才需要关注从有效C ++中发布的报价。 To a first approximation, nobody is doing this anymore. 初步估算,没有人再这样做了。 The people who were doing it before often had their own "enhanced" standard library implementations to support stateful allocators, such as EASTL or BDESTL. 以前这样做的人通常拥有自己的“增强型”标准库实现以支持有状态的分配器,例如EASTL或BDESTL。

Two allocators should compare equal if memory allocated by one can be freed by the other. 如果一个分配器可以释放另一个分配器的内存,则两个分配器应该比较相等。 So, for example, an allocator object that allocates from a pool that it holds can allocate and free memory from that pool, but a different allocator object that has a different pool can't (without a great deal of extra bookkeeping) free memory allocated by the first object. 因此,例如,从其所拥有的池中进行分配的分配器对象可以从该池中分配和释放内存,但是具有不同池的其他分配器对象无法(没有大量额外的簿记)分配已分配的内存由第一个对象。

Making that work right was beyond what the standards committee wanted to take on when allocators were first introduced, which is why the words were so squishy. 正确执行这项工作超出了首次引入分配器时标准委员会要承担的责任,这就是为什么这些词如此糊涂的原因。 And that license has been revoked in more recent versions of the standard. 该许可证在最新版本的标准中已被撤销。

The last paragraph means that if you write an allocator whose objects rely on internal state (eg, the pool allocator that I mentioned above), and the library you're using respects the equality operator (as in, it won't try to pass pointers around among allocators that don't compare equal), your code will break when you try to use it with a different implementation that doesn't pay attention to the equality operator. 最后一段意味着如果您编写一个分配器,该分配器的对象依赖于内部状态(例如,我上面提到的池分配器),并且您使用的库尊重相等运算符(例如,它将不会尝试传递指针在不相等的分配器之间),当您尝试将其与不关注相等运算符的其他实现一起使用时,代码将中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM