DEV Community

Discussion on: User-defined literals

Collapse
 
loki profile image
Loki Le DEV

Great article, user defined literals looks really promising to enforce type safety and avoid conversion errors.

There is one domain where it can help it's when you do physics and you have a lot of units to convert to and from.
For example the code works with radians, but for a human it's easier to write constants or parameters in degrees so you can create a class Angle and the corresponding literals for degrees and radians and it will handle conversions for you.

There is a actually a library making use of this: github.com/mpusz/units
But it needs to be compiled with c++20 standard.

Collapse
 
sandordargo profile image
Sandor Dargo

Thanks for your comment. This seems a promising library, let's see if it gets into the next standard in 3 years!

Also, time to get some C++20 locally to try this. What compiler do you use?

Collapse
 
loki profile image
Loki Le DEV

Well I use g++ but we aren't using this lib nor c++20 yet it's too recent.

Thread Thread
 
sandordargo profile image
Sandor Dargo

I try to install clang today, but the build takes forever and I always get a signal 9. But at least later and later as I add the hacks proposed...