Is there an idiom for a strict typedef in C++, possibly using templates?
Something like:
template <class base_type, int N> struct new_type{
base_type p;
explicit new_type(base_type i = base_type()) : p(i) {}
};
typedef new_type<int, __LINE__> x_coordinate;
typedef new_type<int, __LINE__> y_coordinate;
So I can make something like this a compile time error:
x_coordinate x(5);
y_coordinate y(6);
x = y; // whoops
The __LINE__
in there looks like it might be trouble, but I'd prefer not to have to manually create a set of constants merely to keep each type unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With