C++ When is it OK to extend the `std` namespace?(C++ 什么时候可以扩展 `std` 命名空间?)
问题描述
SO 上的一个帖子说 扩展 std
是 UB (好吧,除非你当然是标准的作家).但有时,std
会被愉快地扩展.什么时候可以这样做?
唯一可以将定义添加到 std
命名空间的情况是对已存在于命名空间中的模板进行特化并显式实例化模板.但是,仅当它们依赖于用户定义的类型时.
[namespace.std](标准草案):
<块引用>如果 C++ 程序将声明或定义添加到命名空间 std 或命名空间 std 内的命名空间,则 C++ 程序的行为是未定义的,除非另有说明.只有当声明依赖于用户定义的类型并且特化满足原始模板的标准库要求并且没有明确禁止时,程序才能将任何标准库模板的模板特化添加到命名空间 std.
李>如果 C++ 程序声明,则其行为未定义
(2.1) 标准库类模板的任何成员函数的显式特化,或
(2.2) 标准库类或类模板的任何成员函数模板的显式特化,或
(2.3) 标准库类或类模板的任何成员类模板的显式或部分特化.
只有当声明依赖于用户定义类型的名称并且实例化符合标准库对原始模板的要求时,程序才能显式实例化标准库中定义的模板.
<小时>
作为标准模板的示例,明确设计用于扩展用户定义的类型:std::hash
和 std::iterator_traits
.
A thread on SO says that extending std
is UB (ok, unless you're the standard writers of course). But from time to time, std
is happily extended. When is it OK to do so?
The only case where it is OK to add a definition into the std
namespace is specialization of a template that already exists in the namespace and to explicitly instantiate a template. However, only if they depend on a user defined type.
[namespace.std] (standard draft):
The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
The behavior of a C++ program is undefined if it declares
(2.1) an explicit specialization of any member function of a standard library class template, or
(2.2) an explicit specialization of any member function template of a standard library class or class template, or
(2.3) an explicit or partial specialization of any member class template of a standard library class or class template.
A program may explicitly instantiate a template defined in the standard library only if the declaration depends on the name of a user-defined type and the instantiation meets the standard library requirements for the original template.
As an example of standard templates that are explicitly designed to be extended for user defined types: std::hash
and std::iterator_traits
.
这篇关于C++ 什么时候可以扩展 `std` 命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 什么时候可以扩展 `std` 命名空间?


- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07