I am writing a CheckedPtr class to practice exception handling (Stroustrup, TC++PL Exercises, 4th Ed., problem 14.1). I want to overload a bunch of operators, and the code to do this is almost the same. I am using macros to avoid being too repetitive, but I know macros are dangerous, so I was wondering if a better method exists.
Here is my code -- the portion shown is part of what I defined within a class called CheckedPtr. Can I do it better, and/or without macros? I would rather not write all of these functions manually, even if it means some risk with macros.
// This is relatively dangerous.
#define CHECKED_PTR_OVERLOAD_COMPARATOR(OP) \
template<typename Ptr> \
bool operator OP(Ptr& p) { return pos OP &*p; }
CHECKED_PTR_OVERLOAD_COMPARATOR(==)
CHECKED_PTR_OVERLOAD_COMPARATOR(<)
CHECKED_PTR_OVERLOAD_COMPARATOR(>)
CHECKED_PTR_OVERLOAD_COMPARATOR(<=)
CHECKED_PTR_OVERLOAD_COMPARATOR(>=)
#undef CHECKED_PTR_OVERLOAD_COMPARATOR