Search code examples
c++c++11user-defined-literals

Does anyone have information on using operator""?


Bjarne Stroustrup gave a keynote presentation today for the Going Native 2012 conference. In his presentation, he discussed the issue of enforcing correct units. His elegant (IMHO) solution to this involved using an operator I have never heard of before: operator"". Using this operator, he was able to write C++ code that looked like this:

ratio = 100m / 1s;

Where operator""m(...) and operator""s(...) were defined.

Does anyone know of any documentation regarding how to actually use this operator (or even if any modern C++ compilers support it)? I tried searching online, but had no luck. Any help would be greatly appreciated.


Solution

  • The syntax you'd be looking for is "user-defined literals" which is a feature of C++11.

    g++ versions 4.7 and better support this feature.

    Here is some documentation describing the use of that operator overload: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2765.pdf

    Also see the excellent link Xeo provides in the comments to your question.