Code examples¶
Short, complete programs that show what using Au actually looks like.
Most of these come in two versions: the way the problem is usually solved in plain C++, and the way it looks with Au. They are presented as tabs so you can flip between them and compare corresponding lines in place — the two versions are kept line-aligned on purpose.
The examples¶
-
Linear speed to revolutions per minute. Converting a wheel’s road speed into revolutions per minute (RPM), without fussing with manual conversion factors like
2πor60. -
Atomic units. Building an entire system of units on top of Au — exact within itself, and as accurate as physics allows at the boundary. Unlike the others, this one has no plain-C++ counterpart: this example shows how to extend Au for a specific domain.
How these examples are written¶
Sharing code examples brings an intrinsic tension. On the one hand, we want to minimize clutter, and emphasize the core ideas and the way the APIs flow together. On the other hand, the examples are how new users will begin using the library, so we can’t compromise on including everything that’s needed to get them running!
Our solution is to distinguish “front matter” from the core of the example. Front matter includes:
#includelines- Individual
usingstatements - Any unit aliases we define for convenience
We’ll always provide this front matter, but we’ll collapse it by default. Look for the “Includes and usings” block inside each tab: expand it and you have the whole file. Nothing on these pages depends on context we have hidden from you, and no snippet is a sketch with the awkward parts elided.
These are also the same conventions we recommend for your code. See Namespaces and includes for our detailed reasoning.
About these examples¶
Every example here is real source code from the
examples/ directory of the
repository, inlined into these pages directly. CI compiles and runs both versions of each one, and
checks that they produce identical output — so the “before” and “after” really do solve the same
problem, and neither can drift out of date with the library.