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
.
相关文章