C++ 2003,第 17.4.3.1/1 节:“程序可以将任何标准库模板的模板特化添加到命名空间 std。标准库模板的这种特化(完全或部分)会导致未定义的行为,除非声明依赖于用户- 定义的外部链接名称,除非特化符合原始模板的标准库要求。"
因此,您可以专门化一个库模板,并将您的专业化放在 namespacestd
中,只要它依赖于用户定义的类型并满足原始模板的要求。
您在编辑的问题中拥有的代码似乎是用户定义名称的专门化,该名称(可能)具有外部链接,因此您不应该对这部分内容有任何问题。
只剩下您的专业化满足原始模板要求的要求。对于您的类型,其中大部分可能微不足道。我可以看到的唯一可能不明显的部分是您似乎确实必须为整个模板提供专业化,而不仅仅是numeric_limits::max()
. 即,您必须执行以下操作(示例应该在 128 位无符号整数类型的范围内):
namespace std {
template <>
class numeric_limits<my_integer> {
public:
static const bool is_specialized = true;
static T min() throw() { return 0;
static T max() throw() { return /* 2^128-1 */; } // ***
static const int digits = 128;
static const int digits10 = 38;
static const bool is_signed = false;
static const bool is_integer = true;
static const bool is_exact = true;
static const int radix = 2;
static T epsilon() throw() { return 0; }
static T round_error() throw() { return 0; }
static const int min_exponent = 0;
static const int min_exponent10 = 0;
static const int max_exponent = 0;
static const int max_exponent10 = 0;
static const bool has_infinity = false;
static const bool has_quiet_NaN = false;
static const bool has_signaling_NaN = false;
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
static T infinity() throw() { return 0; }
static T quiet_NaN() throw() { return 0; }
static T signaling_NaN() throw() { return 0; }
static T denorm_min() throw() { return 0; }
static const bool is_iec559 = false;
static const bool is_bounded = true;
static const bool is_modulo = true;
static const bool traps = false;
static const bool tinyness_before = false;
static const float_round_style round_style = round_toward_zero;
};
}
其中相当一部分是真正用于 FP 类型的,并且不需要对整数类型有意义;我相信它们仍然需要实施。