Best practices around user defined literals

this is more of a general c++ question - is taking advantage of user defined literals considered an ok practice? i only see it in the juce code in the grid section (_px and _fr) but i can certainly see some ways to make my code more readable with this, something like:

123_ms
23_hz

any reason not to take advantage?

Personally, I don’t see how it makes code more readable, especially with those examples, which use decimal integer literals. In those cases, you have to know what operators are defined in the current scope. You need to know if there is an operator “” ms defined with unsigned long long versus a raw literal or a numeric literal, which requires hunting up the defined operator and (for me, at least), reviewing the new rules for how that given type of operator is handled. I just don’t know of an example of code that would be more readable with such a construct. (Obviously, someone thought it would be advantageous, since they added it to the language, but to me it just adds obfuscation.)

1 Like

This a decent talk on user defined literals.

6 Likes