// This is an experiment in wrapping numbers. Why? Because C provides // arrays, pointers, and numbers. In C++, we prefer vector<> to arrays. We prefer ptr<> to raw pointers. So we should prefer number<> to raw numbers. This file shows my current thinking in how you'd want to define numbers. Money, distance, etc. should all be distinct types with different sets of allowed operations. // template struct number { typedef T value_type; value_type value_; number(): value_() {} explicit number(const T& v): value_(v) {} }; template struct comparable { friend bool operator == (const Num& a, const Num& b) { return a.value_ == b.value_; } friend bool operator != (const Num& a, const Num& b) { return a.value_ != b.value_; } }; template struct orderable { friend Num operator < (const Num& a, const Num& b) { return Num(a.value_ < b.value_); } friend Num operator <= (const Num& a, const Num& b) { return Num(a.value_ <= b.value_); } friend Num operator > (const Num& a, const Num& b) { return Num(a.value_ > b.value_); } friend Num operator >= (const Num& a, const Num& b) { return Num(a.value_ >= b.value_); } }; template struct addable { friend Num operator + (const Num& a, const Num& b) { return Num(a.value_ + b.value_); } friend Num operator - (const Num& a, const Num& b) { return Num(a.value_ - b.value_); } }; template struct multiplyable { friend Num operator * (const Num& a, const Num& b) { return Num(a.value_ * b.value_); } }; template struct dividable { friend Num operator / (const Num& a, const Num& b) { return Num(a.value_ / b.value_); } }; template struct scalable { friend Num operator * (const Num& a, Scalar b) { return Num(a.value_ * b); } friend Num operator / (const Num& a, Scalar b) { return Num(a.value_ / b); } friend Num operator * (Scalar a, const Num& b) { return Num(a * b.value_); } }; // Tests struct my_number: number, scalable, scalable, addable, comparable, orderable { explicit my_number(int v): number(v) {} }; #include ostream& operator << (ostream& out, const my_number& num) { return out << num.value_; } int main() { my_number x(5), y(7); cout << x*3 << endl; cout << x/3 << endl; cout << x/3.0 << endl; cout << y*3 << endl; cout << (x == y) << endl; cout << (x != y) << endl; my_number z = x+y; cout << z << endl; cout << x-y << endl; return 0; }