本文介绍了为什么对存储在STL容器中的类重载运算符&()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然在我看到一个声明,C ++标准禁止使用STL容器存储类的元素,如果该类有一个重载运算符&()

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&().

重载运算符&() ,但看起来像一个默认的的操作符可以通过中使用的一组肮脏的模型,被认为是可移植和标准编译。

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.

为了在STL容器中存储的类禁止重载运算符&() $ c> boost :: addressof()解决方法存在?

Why is having an overloaded operator&() prohibited for classes stored in STL containers while the boost::addressof() workaround exists?

推荐答案

,我想在 boost :: addressof()的技巧发明后,要求不要重载一元前缀& 对于在std lib的容器中保存的对象。

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.

我模糊地记得Pete Becker(然后在Dinkumware的标准库实现上工作),声明每个重载运算符地址的人,并期望他们的标准库实现仍然工作应该受到惩罚,必须实现一个标准库这样做。

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.

这篇关于为什么对存储在STL容器中的类重载运算符&()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:43