Skip to content

Instantly share code, notes, and snippets.

@meshell
Created February 22, 2016 22:00
Show Gist options
  • Save meshell/3ed85792516ba6d821d6 to your computer and use it in GitHub Desktop.
Save meshell/3ed85792516ba6d821d6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
// used as conversion
constexpr long double operator"" _m ( long double m ) {
return m;
}
constexpr long double operator"" _km ( long double km ) {
return km * 1000.0_m;
}
constexpr long double operator"" _nmi ( long double nmi ) {
return nmi * 1852.0_m;
}
// used for side-effects
void operator"" _print ( const char* str ) {
std::cout << str << '\n';
}
int main() {
const double distance_sea = 150.0_nmi;
const double distance = distance_sea / 1.0_km;
std::cout << std::fixed << distance_sea << "m\n";
std::cout << std::fixed << distance << "km\n";
0x123ABC_print;
#if __cplusplus > 201103L
// With C++14 some literal operators are defined in the standard library
using namespace std::chrono_literals;
auto d1 = 250ns;
std::chrono::nanoseconds d2 = 1us;
std::cout << "250ns = " << d1.count() << " nanoseconds\n"
<< "1us = " << d2.count() << " nanoseconds\n";
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment