28

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.

Xeo
  • 123,374
  • 44
  • 277
  • 381
Joseph Buntic
  • 263
  • 2
  • 5
  • These are "user-defined literals". They are supported in GCC 4.7. – R. Martinho Fernandes Feb 02 '12 at 20:18
  • @R.Martinho: Not exactly *supported* in Clang. They're recognized syntactically and semantically when called directly, but not if you actually append them on a literal. – Xeo Feb 02 '12 at 20:20
  • 2
    you know what? I think c++ has gone too far, really. I think that code should be easy to read, few people in the world can interpret `ratio = 100m / 1s;` come on. my opinion of course. – vulkanino Feb 02 '12 at 20:22
  • 3
    @vulkanino: [User-defined literals in C++11, a much needed addition or making C++ even more bloated?](http://stackoverflow.com/q/237804/500104) Also, if you don't need them, don't use them. And they're not much different from `f`, `L`, `U`, `ULL` literals in the language. – Xeo Feb 02 '12 at 20:23
  • 8
    @vulkanino So it's just random bashing? – R. Martinho Fernandes Feb 02 '12 at 20:29
  • 2
    @vulkanino "100 m/s" is a fairly standard way to write "100 meters per second"; being able to write "100m / 1s" in C++ gets about as close to the conventional notation as realistically possible without a drastic overhaul of the language, IMO. –  Feb 02 '12 at 20:30
  • User-defined literals are new in C++11, and they are described sufficiently clearly in the actual standards document, which is free to download from GitHub. For the time being, that's probably the best reference. – Kerrek SB Feb 02 '12 at 20:30
  • 8
    @hvd: `auto s = 1s; auto ratio = 100m/s;`. :) – Xeo Feb 02 '12 at 20:32
  • @hvd: "possible without a drastic overhaul of the language" I suspect this will mean drastic overhaul of compilers instead. Also this will introduce many incredible ways to make code unmaintainable. Ah, the possibilities... – SigTerm Feb 02 '12 at 20:38
  • `100m / 1s` should be readable by anyone that took high school physics, if it even requires that much. User defined literals make things more readable in exactly the way that operator overloading makes things more readable. I could be abused, but this is not an example of it. – bames53 Feb 02 '12 at 20:40
  • Ah, user-defined literals. Great, thank you all for the quick responses! – Joseph Buntic Feb 02 '12 at 20:44
  • 1
    Even better, the suffixes don't have to be single characters, so it can be gravity = 9.806meter / 1sec; And having multiple suffixes for a given type would allow implicit unit conversion without having to store the input unit in the type. `1meter == 3.2808ft //evaluates to true, at least for that level of accuracy` – matthias Feb 02 '12 at 21:01
  • @R.Martinho I think his point is more: Even if YOU don't use it, you still have to know the feature and all its pitfalls because clearly you'll often read code written by other people who may use it. – Voo Feb 02 '12 at 22:48
  • 1
    @matthias - Even better: `Velocity v = 100m / 2s; v.FeetPerEpoch();` This is now feasible. – Michael Price Feb 03 '12 at 00:49

2 Answers2

9

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.

Tom B
  • 108
  • 4
  • 15
    C++11 is no less official than C++98 or C++03 or C99. They're all names for the specification, which was published in that year. C++11 was published in 2011, therefore the proper term for it is C++11. – Nicol Bolas Feb 02 '12 at 20:44
  • Could have sworn I revisited this and cleaned that up. You're correct, and I've revised it. – Tom B May 02 '14 at 21:01
3

Currently the best documentation is probably in the standard itself. You can get the latest version from the commitee's site. According to gcc's site it will be in the next revision of gcc (gcc-4.7). You should be able to test it when building gcc from the SVN repository.

Dietmar Kühl
  • 141,209
  • 12
  • 196
  • 356