什么可以和我不能专门在std命名空间? [英] What can and can't I specialize in the std namespace?
问题描述
用户可以向 std
命名空间添加显式专门化。
有什么模板可以和我不能专业化吗?
从标准中松散:
-
numeric_limits
不适用于非算术标准类型(例如complex< T>
) -
shared_ptr
的属性应为CopyConstructible,CopyAssignable和LessThanComparable [and]可转换为bool
。
-
weak_ptr
-
[T] emplate specializations [of
std :: hash
]必须满足类模板哈希 -
< type_traits>
中的任何内容:对于本子条款中定义的任何类模板,除非另有说明,否则为未定义。 (仅明确允许common_type
的一些专业化) -
-
istreambuf_iterator
的所有专业化应具有一个简单的复制构造函数,一个constexpr默认构造函数 -
对任何类型的其他类型实例化类模板
complex
的效果比float,double或long double都未指定。 -
atomic
-
类模板
模板必须具有已删除的副本构造函数,已删除的副本赋值运算符和constexpr值构造函数。 unary_function
和binary_function
不推荐使用。程序不应声明这些模板的特殊化。
当然,总体条款17.6.4.2.1,句子1(感谢@sehe和@curiousguy):
< blockquote>
除非另有说明,否则如果C ++程序将声明或定义添加到命名空间std或命名空间std中的
命名空间,那么它的行为是未定义的。程序可以将任何标准库模板的模板专用化
添加到命名空间std中,只要声明依赖于用户定义的类型
,并且专业化满足原始模板的标准库要求,并且不明确
禁止。
而句子2:
如果声明了
,那么C ++程序的行为是未定义的。标准库类模板或
标准库类或类模板的任何成员函数模板的显式特化,
标准库类或类模板的任何成员类模板的显式或部分专门化。
程序可以仅在声明
取决于用户定义类型的名称并且实例化满足标准库要求
时才显式实例化在标准库中定义的模板原始模板。
Users are allowed to add explicit specializations to the std
namespace. However, there are a few templates that I am explicitly forbidden from specializing.
What templates can and can't I specialize?
Quoting loosely from the standard:
numeric_limits
shall not be specialized for non-arithmetic standard types (e.g.complex<T>
)"[S]pecializations of
shared_ptr
shall be CopyConstructible, CopyAssignable, and LessThanComparable [and] convertible tobool
.""Specializations of
weak_ptr
shall be CopyConstructible and CopyAssignable.""[T]emplate specializations [of
std::hash
] shall meet the requirements of class template hash."Anything in
<type_traits>
: "The behavior of a program that adds specializations for any of the class templates defined in this subclause is undefined unless otherwise specified." (only some specializations ofcommon_type
are explicitly allowed)Locales have certain required specializations.
All specializations of
istreambuf_iterator
shall have a trivial copy constructor, a constexpr default constructor, and a trivial destructor."The effect of instantiating the class template
complex
for any type other than float, double, or long double is unspecified." I take it that means that defining such other specializations is pointless."Specializations and instantiations of the
atomic
template shall have a deleted copy constructor, a deleted copy assignment operator, and a constexpr value constructor.""The class templates
unary_function
andbinary_function
are deprecated. A program shall not declare specializations of these templates."
And of course the overarching clause 17.6.4.2.1, sentence 1 (thanks @sehe and @curiousguy):
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.
And sentence 2:
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 declaration depends on the name of a user-defined type and the instantiation meets the standard library requirements for the original template.
这篇关于什么可以和我不能专门在std命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!