且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是专门化std :: numeric_limits< T>对于用户定义的数字类?

更新时间:2022-05-12 05:50:04

简答:



继续,没有什么不好会发生。

Short answer:

Go ahead, nothing bad will happen.

C ++标准广泛地保护C ++标准中的 :: std +11 17.6.4.2.1,但特别允许你的情况在第1和第2段:

The C++ standard extensively protects the ::std namespace in C++11 17.6.4.2.1, but specifically allows your case in paragraphs 1 and 2:


C ++程序的行为是未定义的if它将声明或定义添加到命名空间std或命名空间std中的
命名空间,除非另有规定。程序可以将任何标准库模板的模板专用化
添加到命名空间std中,只要声明依赖于用户定义的类型
,并且专门化满足原始模板的标准库要求,并且不明确
禁止。

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.

[...]程序可以显式实例化标准库中定义的模板,只有声明
依赖于名称

[...] 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.

较早的C + C语言的用户定义类型和实例化满足标准库要求
。 +03在17.4.3.1/1中有一个类似的定义:

The older C++03 has a similar definition in 17.4.3.1/1:


它是未定义的C ++程序添加声明或定义到命名空间std或命名空间std中的
,除非另有说明。程序可以将任何
标准库模板的模板特化添加到命名空间std。标准
库模板的这种特殊化(完全或部分)导致未定义的行为,除非声明取决于用户定义的
外部链接的名称,并且除非专业化满足原始模板。

It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a user-defined name of external linkage and unless the specialization meets the standard library requirements for the original template.

经过这个基本的垫脚石,你已经指出,C ++ 03 18.2.1 / 4禁止特殊化 :: std :: numeric_limits

After getting past this fundamental stepping stone, you already pointed out, C++03 18.2.1/4 forbids specializations of ::std::numeric_limits for certain types:


非基本标准类型,如复杂(26.2.2),不应有专业化。

Non-fundamental standard types, such as complex (26.2.2), shall not have specializations.

当前更多的C ++ 11 18.3.2.1/4具有稍微不同的措辞:

The more current C++11 18.3.2.1/4 has a slightly different wording:


非算术标准类型,例如 complex< T> (26.4.2),不应有专业化。

Non-arithmetic standard types, such as complex<T> (26.4.2), shall not have specializations.

T 是,因为你自己定义(因为@BoPersson已经在评论中指出)。

Both of these formulations however allow specializations for non-standard types, which T is, since you defined it yourself (as @BoPersson already pointed out in the comments).

C ++ 11 18.3.2.3/1提示您应该(但不要求您)确保您的专业化包含所有成员。

C++11 18.3.2.3/1 hints that you should (but does not require you to) ensure that your specialization has all members.

此外,您可能希望确保C ++ 11 18.3.2.3/2不被您的专业化违反:

Also, you may wish to ensure that C++11 18.3.2.3/2 is not violated by your specialization:


cv限定类型cv T上numeric_limits的特殊化的每个成员的值应等于
到非限定类型T上的专业化的相应成员的值。

The value of each member of a specialization of numeric_limits on a cv-qualified type cv T shall be equal to the value of the corresponding member of the specialization on the unqualified type T.

这本质上意味着,如果你想专门为 T 因此 T const T volatile T const volatile

Which essentially means, that if you wish to specialize it for T, you should also do so for T const, T volatile and T const volatile.