Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ available "literal suffix code" for units

C++1x supports literal suffixes (cmp. e.g. http://ecn.channel9.msdn.com/events/GoingNative12/GN12Cpp11Style.pdf). I am using gcc 4.7 and want to introduce some units for our system. Most notably half of our code uses degrees and the other half radians (due to various 3rd party libraries), and obviously this is always a constant cause of mistakes. Being able to say e.g. "Radian angle = 90_deg;" would be so helpful.

I've looked into how to implement this and it looks doable, however it will take some time to get everything right. So I wonder whether there is a finished/tested implementation out there that already implements all this that can be used (no need for every C++ developer to re-implement that, is there?). Aside from rad/deg I am looking for length measurements (mm, cm, m). I've already googled but did not find anything usable.

Does anybody know an implementation of e.g. the SI system that can be used?

like image 935
Frankie Avatar asked Nov 10 '12 08:11

Frankie


1 Answers

Use boost.units.

You will get the expected benefit: being able to safely deal with metrics expressed in different units.
The syntax with boost units is not that bad:

quantity<length>    dx(2.0*meter);
like image 139
log0 Avatar answered Nov 09 '22 13:11

log0