简体   繁体   English

为什么对存储在STL容器中的类禁止重载operator&()?

[英]Why is overloading operator&() prohibited for classes stored in STL containers?

Suddenly in this article ("problem 2") I see a statement that C++ Standard prohibits using STL containers for storing elemants of class if that class has an overloaded operator&() . 突然在本文中(“问题2”)我看到一个声明,如果该类有一个重载的operator&() ,C ++标准禁止使用STL容器来存储类的元素。

Having overloaded operator&() can indeed be problematic , but looks like a default "address-of" operator can be used easily through a set of dirty-looking casts that are used in boost::addressof() and are believed to be portable and standard-compilant. 有重载的operator&() 确实有问题 ,但看起来像一个默认的“地址”运算符可以很容易地通过boost::addressof()中使用的一组看起来很脏的转换来使用,并且被认为是可移植的标准compilant。

Why is having an overloaded operator&() prohibited for classes stored in STL containers while the boost::addressof() workaround exists? boost::addressof()变通方法存在时,为什么对存储在STL容器中的类禁止重载operator&()

Without having looked at the links, I suppose the tricks in boost::addressof() were invented well after the requirement to not to overload unary prefix & for objects to be held in containers of the std lib. 在没有查看链接的情况下,我认为boost::addressof()中的技巧是在要求不重载一元前缀&将对象保存在std lib的容器中之后发明的。

I vaguely remember Pete Becker (then working for Dinkumware on their standard library implementation) once stating that everyone who overloads the address-of operator and expects their standard library implementation still to work should be punished by having to implement a standard library which does this. 我依旧记得Pete Becker(后来为Dinkumware的标准库实现工作)曾经说过,每个重载地址运算符并希望他们的标准库实现仍然有效的人都应该通过实现这样做的标准库来惩罚。

可能因为禁止使用重载的operator&()类而不是创建std :: addressof()函数并用它替换容器代码中的&的每次使用都不那么麻烦。

The standard was finalized in 1998 with fixes in 2003, whereas boost::addressof dates to early 2002. 该标准于1998年完成,修订于2003年,而boost::addressof 日期为 2002年初。

Moreover, it's not clear that addressof is the answer. 此外,尚不清楚addressof是否是答案。 Overloads of operator&() indicate that raw pointers are supposed to be avoided. operator&()重载表明应该避免使用原始指针。 The Allocator::address member provides the best interface to get from Allocator::reference to Allocator::pointer , so in general theory, you should be able to effectively introduce an operator& override to an otherwise well-behaved class with a custom allocator. Allocator::address成员提供了从Allocator::referenceAllocator::pointer的最佳接口,因此在一般理论中,您应该能够有效地 operator&覆盖引入具有自定义分配器的其他良好行为的类。

Considering that references do almost everything that pointers do, and the Allocator interface abstracts everything else, there should be no need for raw pointers. 考虑到引用几乎完成指针的所有操作,并且Allocator接口抽象其他所有内容,因此不需要原始指针。

Convenience to the library implementers should not be an issue. 图书馆实施者的便利应该不是问题。 The ill-defined semantics of Allocator::pointer are a problem, and what I've read so far in C++0x doesn't clear that up. Allocator::pointer的定义不明确的语义是一个问题,到目前为止我在C ++ 0x中读到的内容并没有清楚。

C++0x removes any mention of operator& from CopyConstructible, and furthermore doesn't require anything-Constructible for container arguments at all — the user can stick to emplace . 的C ++ 0x消除了任何提及operator&的拷贝构造,而且不需要任何东西,Constructible集装箱参数在所有-用户能坚持到emplace Even vector only requires Destructible, although I suppose actually using insert or erase would require more. 偶数vector只需要Destructible,虽然我认为实际使用inserterase需要更多。

(Note that, in the strictest reading, overloads are not forbidden in C++03. You are just not allowed to change the value or type of the builtin.) (请注意,在最严格的读取中,C ++ 03中不禁止重载。您不能更改内置的值或类型。)

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

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