3

I stumbled upon the following example while browsing the C++ Core Guidlines document:

Example

change_speed(double s);   // bad: what does s signify?
// ...
change_speed(2.3);

A better approach is to be explicit about the meaning of the double (new speed or delta on old speed?) and the unit used:

change_speed(Speed s);    // better: the meaning of s is specified
// ...
change_speed(2.3);        // error: no unit
change_speed(23m / 10s);  // meters per second

We could have accepted a plain (unit-less) double as a delta, but that would have been error-prone.

With regard to that last line of code, there was no mention on that specific page what that syntax meant, and it looked positively alien to me.

After several hours wasted invested figuring out this is probably a standard library predefined 'user-defined' literal and learning more about them, I tried to find out where this particular literal is defined, but while 's' is mentioned here along with a handful of other literals, I found no information on 'm'.

There is also this question on SO, but I think the answers there seem to be completely out of date.

Q: Where is the standard-library predefined user-defined literal "m" defined*?


* Sweet, sweet alliteration.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57

1 Answers1

5

It is not a standard user defined literal

The list of standard user defined literals can be found here at the bottom: http://en.cppreference.com/w/cpp/language/user_literal

And operator""m is not one of them, as the standard library does not deal with units of length (yet).

Fatih BAKIR
  • 4,569
  • 1
  • 21
  • 27
  • Thanks Fatih. I've actually already linked to that link in the question :) So if `23m` is not a standard library literal, what is this? Or if it _is_ a user-defined literal but just not a standard library one, why is it allowed without an underscore? – Tasos Papastylianou Jul 20 '17 at 23:54
  • Or are you just saying Bjarne probably invented a literal that isn't in the standard library yet, just for the purpose of that example? – Tasos Papastylianou Jul 20 '17 at 23:55
  • 2
    @TasosPapastylianou, well, Bjarne in fact likes to make some functionality up so that some examples are clearer (like using ranges in his C++11/14 book). As you've said, it is not allowed to be used without an underscore. So, until we get standard units of length, that code is ill formed. – Fatih BAKIR Jul 21 '17 at 00:15