C++11 User Defined Literals for Representing Units of Time
C++11 allows you to specify user defined literals which finally gives the programmer the ability to define their own literals. For example, by default when you write 1.2F that means 1.2 float, and when you write 100ULL that means 100 unsigned long long. However, not only does this construct give you the ability to express different formats and suffixes, it also gives you the ability to manipulate what the actual value means.
This particular example is almost Ruby on Rails ActiveSupport like.
The following example always returns time in milliseconds, but allows the programmer to specify time values in units other than milliseconds. Surely this can be abused and there are way more powerful things you can do than what this example does. However, I think this is a very realistic example and makes the code cleaner and easier to read. No more putting (60 * 60 * 1000) type expressions scattered around the code. Did I mention less error prone?
#include <iostream> using namespace std; constexpr unsigned long long operator "" ms (unsigned long long n) { // nothing to do return n; } constexpr unsigned long long operator "" sec (unsigned long long n) { // convert seconds to milliseconds return n * 1000ULL; } constexpr unsigned long long operator "" min (unsigned long long n) { // convert minutes to milliseconds return n * 60ULL * 1000ULL; } constexpr unsigned long long operator "" h (unsigned long long n) { // convert hours to milliseconds return n * 60ULL * 60ULL * 1000ULL; } int main() { cout << 20ms << endl; cout << 20sec << endl; cout << 20min << endl; cout << 20h << endl; cout << 1h + 5min + 3sec << endl; return 0; }
The output of the above code.
$ ./literals 20 20000 1200000 72000000 3903000
2 Comments