I often use the Boost strong typedef utility to improve the safety of my programs. For example by writing code like this:
BOOST_STRONG_TYPEDEF(int, X)
BOOST_STRONG_TYPEDEF(int, Y)
BOOST_STRONG_TYPEDEF(int, Width)
BOOST_STRONG_TYPEDEF(int, Height)
struct Rect {
Rect(X x, Y y, Width w, Height h);
};
// Usage:
Rect rect(X(10), Y(20), Width(800), Height(600));
The strong typedef here improves both code readability and safety. (The compiler will report an error if the arguments are provided in the wrong order, which would not have been the case if the arguments were all int
.)
My questions are:
- Is it ok to use BOOST_STRONG_TYPEDEF for this purpose? (The documentation is very brief.)
- Are there important reasons to prefer the boost parameter library instead?