0

The user-defined-literals from C++11 helps the integers and float numbers have certain units like m, cm, mm, km, and so on. But it seems that it deals only with constant. I have a runtime program which always calculates some lengths and convert them between different units. Are there also similar mechanisms like user-defined-literals in C++ for variables?

T.s. Arun
  • 317
  • 5
  • 17
Summer Fang
  • 286
  • 3
  • 14
  • 8
    Those are called types. You can use a [library](https://github.com/nholthaus/units) if you don't want to write them yourself. – nwp Aug 28 '17 at 13:33
  • 1
    Are you looking for something like [boost::units](http://www.boost.org/doc/libs/1_65_0/doc/html/boost_units.html)? – G.M. Aug 28 '17 at 13:37
  • 1
    Could you be more precise and share an example? – King Thrushbeard Aug 28 '17 at 14:13
  • 1
    The idea of "user-defined-literals...for variables" makes no sense. The word "literal" means, the in-source-code representation of a particular _value_. It's practically a synonym for "constant". I agree with nwp: It sounds like you are asking how to define specialized numeric types. – Solomon Slow Aug 28 '17 at 14:25

1 Answers1

0

To my knowledge, the C++14 standard provides literal operators for complex numbers, string, string_view and std::chrono time values.. There are no operators defined for other SI units, and there no classes supporting this in the standard.

There are libraries to define unit systems, e.g. Boost.Units. However, as far I read the docs, Boost.Units does not defined user-defined literal operator. But then it should be easy to do it on your own.

Jens
  • 9,058
  • 2
  • 26
  • 43