Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)
问题描述
突然在这篇文章(问题 2") 我看到一个声明,即 C++ 标准禁止使用 STL 容器来存储类的元素,如果该类具有重载的 operator&()代码>.
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&()
.
重载 operator&()
确实有问题,但看起来可以通过一组看起来很脏的演员表在 boost::addressof()
中使用,被认为是可移植和符合标准的.
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.
为什么在存在 boost::addressof()
解决方法的情况下禁止存储在 STL 容器中的类使用重载的 operator&()
?
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 的标准库实现工作)曾经说过,每个重载 address-of 运算符并希望他们的标准库实现仍然有效的人都应该受到惩罚,必须实现一个标准库做这个.
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 容器中的类重载 operator&()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么禁止对存储在 STL 容器中的类重载 operator&()?


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01