gpt4 book ai didi

c++ - 我可以和不可以专注于 std 命名空间吗?

转载 作者:IT老高 更新时间:2023-10-28 13:24:23 26 4
gpt4 key购买 nike

允许用户向 std 命名空间添加显式特化。但是,有一些模板是明确禁止我专门研究的。

哪些模板可以专攻,哪些不能专攻?

最佳答案

粗略地引用标准:

  • numeric_limits不应专门用于非算术标准类型(例如 complex<T>)

  • “[S] shared_ptr 的专用化应为 CopyConstructible、CopyAssignable 和 LessThanComparable [并且] 可转换为 bool。”

  • weak_ptr 的特化应该是 CopyConstructible 和 CopyAssignable。”

  • "[T]模板特化[std::hash]应满足类模板哈希的要求。"

  • <type_traits> 中的任何内容:“除非另有说明,否则为本子条款中定义的任何类模板添加特化的程序的行为是未定义的。” (只有 common_type 的一些特化是明确允许的)

  • 语言环境具有某些必需的特化。

  • istreambuf_iterator 的所有专业应该有一个普通的复制构造函数、一个 constexpr 默认构造函数和一个普通的析构函数。

  • “为浮点、 double 或长 double 以外的任何类型实例化类模板 complex 的效果未指定。”我认为这意味着定义这样的其他专业是没有意义的。

  • atomic 模板的特化和实例化应具有已删除的复制构造函数、已删除的复制赋值运算符和 constexpr 值构造函数。”

  • “类模板 unary_functionbinary_function 已弃用。程序不得声明这些模板的特化。”

当然还有首要条款 17.6.4.2.1,第 1 句(感谢 @sehe 和 @curiousguy):

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to anamespace within namespace std unless otherwise specified. A program may add a template specializationfor any standard library template to namespace std only if the declaration depends on a user-defined typeand the specialization meets the standard library requirements for the original template and is not explicitlyprohibited.

第二句:

The behavior of a C++ program is undefined if it declares

  • an explicit specialization of any member function of a standard library class template, or

  • an explicit specialization of any member function template of a standard library class or class template, or

  • 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 declarationdepends on the name of a user-defined type and the instantiation meets the standard library requirementsfor the original template.

关于c++ - 我可以和不可以专注于 std 命名空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8513417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com