4 ms·
In C++ units can, and often are, implemented on top of the existing type system. See std::duration/time_point specifically for times, and boost.units for gener
by gpderetta 5y ago
In C++ units can, and often are, implemented on top of the existing type system.
See std::duration/time_point specifically for times, and boost.units for generalized unit support.
It is implemented via (templated) wrappers, but they are very close to zero overhead if not completely free. User defined literals also allow for a very succinct syntax, but to be honest, I usually don't bother. I normally write:
auto delay = std::chrono::seconds{3};
instead of:
auto delay = 1s;
- deleted 5y ago[deleted]
- ChrisLomont 5y agoIn C++ you can use user defined literals to make the units stuff compile time checked, similar to this https://github.com/bernedom/SI https://github.com/bernedom/SI for example.
- roeles 5y agoUsing templates I once implemented logic for dealing with SI units[1]. I also included angles, which is a bit odd but useful in practice. It is really nice to have a type system that checks your formulas. Saved me a couple of times. [1] https://gitlab.com/roeles/zen/-/blob/master https://gitlab.com/roeles/zen/-/blob/master
- roeles 5y agohttps://gitlab.com/roeles/zen/-/blob/master/src/physics/unit.hpp https://gitlab.com/roeles/zen/-/blob/master/src/physics/unit...