18

コンパレータも特殊化もせずに、独自の型を作成しました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;
}
4

1 に答える 1