Unit-Safe Physical Quantities

Medium C++ Validation Asked at Siemens
Solve this problem

Encode physical units in the type system so adding volts to amperes is a compile error, and dividing volts by amperes yields ohms.

Constraints

  • Template on integer exponents for voltage, current and time.
  • Addition and subtraction only between identical unit types.
  • Multiplication and division must add and subtract exponents.
  • All arithmetic constexpr.
  • No runtime overhead over a bare double.
  • Provide literal helpers for volts and amperes.
Starter codeC++
1template <int V, int A, int S>2struct Quantity {3    double value;4    // TODO5};