I want to be able to use compile time information to prevent two objects of the same class being combined. In particular if a
represents meters, and b
uses centimeters I want the compiler to not allow their combination.
Can I do this with protected inheritance? Do I need to use Tags?
Something like this?
struct Meters{};
struct Centimeters{};
template < typename Units >
class DimensionedLength : protected Length {
// What do I need to put in here?
}
int main(){
{
Length a(0.0), b(1.0), c(2.0);
std::cout << a+b << std::endl; // fine
std::cout << a+c << std::endl; // fine
}
{
DimensionedLength<Meters> a(0.0), b(1.0);
DimensionedLength<Centimeters> c(2.0);
std::cout << a+b << std::endl; // fine
std::cout << a+c << std::endl; // compile error!
}
return 1;
}