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&()?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
