我创建了自己的类型,没有任何比较器,也没有std::numeric_limits
. 尽管如此,由于某种原因,std::numeric_limits<MyType>
编译得很好。为什么 c++ 标准委员会将numeric_limits
模板定义为对所有类型都有效,包括非数字类型?
下面的示例代码:
#include <iostream>
#include <limits>
using namespace std;
// This is an int wrapper that defaults to 666 instead of 0
class A {
public:
int x;
public:
A() : x(666) {}
};
int main() {
A a = std::numeric_limits<A>::max();
A b = std::numeric_limits<A>::max();
std::cout << a.x << "\n" << b.x;
// your code goes here
return 0;
}