C++ 什么时候可以扩展 `std` 命名空间? [英] C++ When is it OK to extend the `std` namespace?

查看:26
本文介绍了C++ 什么时候可以扩展 `std` 命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO 上的一个帖子说 扩展 std 是 UB (好吧,除非你当然是标准的作家).但有时,std 会被愉快地扩展.什么时候可以这样做?

解决方案

唯一可以将定义添加到 std 命名空间的情况是对已存在于命名空间中的模板进行特化并显式实例化模板.但是,仅当它们依赖于用户定义的类型时.

[namespace.std](标准草案):

<块引用>

  1. 如果 C++ 程序将声明或定义添加到命名空间 std 或命名空间 std 内的命名空间,则 C++ 程序的行为是未定义的,除非另有说明.只有当声明依赖于用户定义的类型并且特化满足原始模板的标准库要求并且没有明确禁止时,程序才能将任何标准库模板的模板特化添加到命名空间 std.

    李>
  2. 如果 C++ 程序声明,则其行为未定义

    (2.1) 标准库类模板的任何成员函数的显式特化,或

    (2.2) 标准库类或类模板的任何成员函数模板的显式特化,或

    (2.3) 标准库类或类模板的任何成员类模板的显式或部分特化.

    只有当声明依赖于用户定义类型的名称并且实例化符合标准库对原始模板的要求时,程序才能显式实例化标准库中定义的模板.

<小时>

作为标准模板的示例,明确设计用于扩展用户定义的类型:std::hashstd::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):

  1. 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.

  2. 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` 命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆