Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason not to use unit enforcing types?

Conceptually it seems to me that using unit enforcing based types (Meters, Seconds, Kilograms) would have massive benefits (extra checking in passing args, getting rid of unit names in vars, etc) and yet I've not run into as much code which does. And the code that I've seen that does has used custom types.

I see that boost has a units library (boost::units simply enough) and yet, I don't see much evidence of it being widely used (in a basic google search).

Is there a good reason for this?

Together these seem to imply that there must be some reason the practice has not been as widely adopted as I'd expect. Perhaps more trouble than they're worth for some reason?

And so I ask:

Is there a reason not to use unit enforcing types? And specifically is there are reason not to use boost::units?

like image 681
Catskul Avatar asked Sep 25 '12 16:09

Catskul


1 Answers

I think the main reason why this technique isn't more prevalent is that it's hideously difficult and cumbersome to spell out and to read.

Hopefully this will finally become a more accepted programming style with C++11, which adds user-defined literals to the language which let you write:

auto acc = 10_m / 1_s / 1_s;

rather than the traditional

myframework::units::si<acceleration>::type acc = myframework::unit_cast<units::meters>(10.0)
   / myframework::unit_cast<units::seconds>(1)
   / myframework::unit_cast<units::seconds>(1);
like image 101
Kerrek SB Avatar answered Sep 18 '22 17:09

Kerrek SB