6

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?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Frankie
  • 653
  • 1
  • 9
  • 20
  • 1
    [This reference](http://en.cppreference.com/w/cpp/language/user_literal) has a couple of examples. – Some programmer dude Nov 10 '12 at 09:05
  • Thanks, but I am not looking for examples/references. I am confident I could implement a full set of literals and cast operators to handle my needs. However this will take time to implement, and more to test all use cases. As the SI system is the same for everybody, I would assume that hundreds of C++ developers do something like that - so I am hoping that somebody has done a clean and tested implementation that can "just" be used. I do not want to re-invent the wheel. – Frankie Nov 10 '12 at 09:09
  • 3
    +1 for not wanting to re-invent the wheel. This is **not** what you are looking for but Boost offers a templatized version under [Boost.Units](http://www.boost.org/doc/libs/1_52_0/doc/html/boost_units.html). I am curious what other answers you will get. – Ali Nov 10 '12 at 09:51
  • Five seconds of google search lead me to [this](http://www.codeproject.com/Articles/447922/Application-of-Cplusplus11-User-Defined-Literals-t). Available under [CPOL](http://www.codeproject.com/info/cpol10.aspx). "scientific_units_and_binary.cpp" seems useful to your needs. –  Nov 10 '12 at 10:14
  • Okay, it was just the first thing I thought about. But a possible reason it's not common may be that it's so new and not many compilers have supported it for very long. – Some programmer dude Nov 10 '12 at 12:02
  • @llonesmiz: I found that too, but it's not complete. It misses at the least the cast operators. – Frankie Nov 11 '12 at 07:08
  • `I've already googled but did not find anything usable.` Well, that's all we're going to be able to do for you, too! I'm confident that the majority of answers to the majority of research-related questions are predominantly Google-driven, albeit, yes, with some intuition and experience to steer the choice of keywords and the scanning through results. And maybe sometimes a search results cache in the brain. – Lightness Races in Orbit Dec 06 '12 at 15:05

1 Answers1

2

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);
log0
  • 10,489
  • 4
  • 28
  • 62
  • @bames53 I didn't want to say that. It is just that boost.Units does not provide UDL. – log0 Nov 13 '12 at 15:27
  • You mention that I get the same benefits. However assuming I find a C++11x implemention, why would Boost be preferable? – Frankie Dec 01 '12 at 23:58
  • @Frankie Ok sorry it seems that my answer was definitely not clear enough ) Boost will provides what you want: A library to ease/secure manipulation of units in SI system. However the boost library does not provides UDL (which is syntactic sugar anyway). Why should you uses Boost? Because Boost tends to be an incubator for the c++ standard library. The quality standard are very high, libraries are tested and it has an important community of users. – log0 Dec 03 '12 at 10:39
  • 1
    I have implemented a simple set of user defined literals for boost units including all S.I. prefixes [here](https://github.com/acecil/units_literals/) –  Feb 06 '13 at 13:56
  • There's also [PhysUnits-CT-Cpp11](https://github.com/martinmoene/PhysUnits-CT-Cpp11), a small C++11, C++14 header-only library for compile-time dimensional analysis and unit/quantity manipulation and conversion. Simpler than Boost.Units, only depends on standard C++ library, SI-only, integral powers of dimensions – Martin Moene Jul 02 '16 at 17:00