Search code examples
c++operator-overloadingoperatorsc++-chrono

Operators in c++ chrono


What are these operators in C++ chrono : s 10s , ns 10ns , ms 10 ms , h 10h, min 10 min I know that in 10s means 10 seconds, ms means milliseconds, and so on.

Can I use letters as operators, or can I overload them?


Solution

  • Those are User Defined Literals. Yes, you can define your own.

    In a product I work on, we have defined several literals. For example, one is named _qs for QString literals, so we can write "meow"_qs and that is then a QString object - so we can do, for example:

    const int num_cats = 42;
    const auto foo = "we have %1 cats"_qs.arg(num_cats);
    

    and foo will be a QString containing the text "we have 42 cats". Quite handy.

    Note that only the implementation can define names that do not start with underscore (_). All your own literals must start with _.