std::addressof是否否定了STL运算符和要求?
Does std::addressof negate the STL operator& requirement?
为了让一个类型在C++03标准库中"玩得很好",重载operator&()
被认为是个坏主意,因为地址需要正确使用该类型,而重载会产生意想不到的问题;这里的经典例子是CCD_ 2。
- 随着
std::addressof()
在C++11中的出现,这是否否定了对标准库中使用的类型的旧要求 - 该要求是否在C++11中的标准中明确说明(或从中删除),即规范是否要求标准库使用
std::addressof()
容器的value_type
只有几个要求。它们主要取决于容器,但对于一般情况,要求至少是MoveConstructible
和MoveAssignable
。
查看C++11标准表了解这些要求的含义,您会得到以下信息:
§17.6.3.1表20(MoveConstructible
):
+----------------------------------------------------------+
| Expression | Post Condition |
|----------------------------------------------------------+
| T u = rv; | u is equivalent to the |
| | value of rv before the construction |
+----------------------------------------------------------+
| T u(rv); | T(rv) is equivalent to the |
| | value of rv before the construction |
+----------------------------------------------------------+
| rv's state is unspecified. |
| |
+----------------------------------------------------------+
§17.6.3.1表22(MoveAssignable
)。
+-----------+--------------+-------------+----------------------+
| Expression| Return Type | Return value| Post Condition |
|-----------|--------------|-------------|----------------------|
| t = rv; | T& | t | t is equivalent to |
| | | | the value of rv |
| | | | before the assignment|
| | | | |
+---------------------------------------------------------------+
| rv's state is unspecified |
| |
+---------------------------------------------------------------+
容器有更多的要求,这取决于它是什么,比如DefaultConstructible
。但是,这两个要求都不要求operator&
过载。它们主要处理value_type
的可构造性,而不是类型提供的运算符重载。
您可以在§17.6.3.1中找到其他表格。§23中规定了集装箱要求。
C++03 CopyConstructible要求明确包括运算符地址返回对象实际地址的要求,如注释§20.1.3(表30)所述,因此重载该运算符的类型可能会遇到标准库问题。
+------------+-------------+--------------------------+
| expression | return type | requirement |
+------------+-------------+--------------------------+
| T(t) | | t is equivalent to T(t) |
+------------+-------------+--------------------------+
| T(u) | | u is equivalent to T(u) |
+------------+-------------+--------------------------+
| t.~T() | | |
+------------+-------------+--------------------------+
| &t | T* | denotes the address of t |
+------------+-------------+--------------------------+
| &u | const T* | denotes the address of u |
+------------+-------------+--------------------------+
C++11简化了移动(以及复制可构造和可分配的定义)到更基本的语法;
T ( rvalue ); // move construct
T u = rvalue; // move assign
T ( value ); // copy construct
T u = value; // copy assign
它没有提及运算符的重载地址,但也很少提及明确需要std::addressof
(在某些.resize()
函数之外)。然而,即使在存在过载的CCD_ 16的情况下,CCD_ 15也明确要求类型的地址是正确的。
总之,规范中可能很少强制使用std::addressof()
,但考虑到简化的要求和明确的对象地址要求;CCD_ 18(或类似物)的使用非常接近于被强制使用。
我的收获是;如果您正在编写泛型代码,并且需要对象的地址,请使用std::addressof()
并坚持安全起见。
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 如果我真的真的想从 STL 容器继承,并且我继承构造函数并删除新运算符,会发生什么?
- 如何重载下标运算符 [] 以引用 2d STL 数组?
- 默认赋值运算符如何在实际 STL 中实现
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- ostream 运算符<< 为获取 STL 容器而过载,传递 std::string 会破坏它?
- 如果键不存在,使用 [] 运算符访问 STL Map 元素会添加新元素
- STL按客户"<"运算符对向量进行排序。为什么要将"<"运算符定义为 const?
- 重载运算符<<() 用于 stl 容器
- STL std::map 运算符 [] 在赋值的右侧
- 重新定义要在字符串的 STL 算法中使用的<运算符
- STL 类型的 Google 测试和运算符<<重载
- C++ STL 比较映射找不到(运算符==)
- C++STL字符串运算符+关联性
- 关于在 STL 中使用范围解析运算符的困惑
- 与C STL容器[]运算符和默认值混淆
- C++ STL 关系运算符如何比较堆栈
- 重载运算符运算符<在我的类中没有被 STL 使用max_element 标准 Haskell 函数 :: (a -> 也许 a) -> a -> [a]
- 最佳 STL 变换 - 类似于三元运算符的模板函数
- 重载 [ ] 运算符用于 C++ 中的 stl 映射