#include #include #include #include #define MYTEST_DYNAMIC 1 #define MYTEST_CONSTEXPR 1 static void print( const char* const myname, const char* const name, const char* const abbr ) { std::cout << '\n' << myname << ":\n" << "Name: \"" << name << "\"\n" << "Abbr: \"" << abbr << "\"\n" ; } #if MYTEST_CONSTEXPR static constexpr auto describe_units() { constexpr auto describe = [](const char* myname, const auto& v) constexpr { return std::tuple{ myname, v.name(), v.abbreviation(), // units::name(v), // units::abbreviation(v), }; }; using namespace units::literals; constexpr units::length::meter_t distance {1_m}; constexpr units::angle::degree_t angle_deg {30_deg}; constexpr units::angle::radian_t angle_rad {30_deg}; constexpr units::dimensionless::scalar_t scalar {5}; return std::array { describe("distance", distance), describe("angle_deg", angle_deg), describe("angle_rad", angle_rad), describe("scalar", scalar), describe("pi", units::constants::pi), describe("c", units::constants::c), describe("G", units::constants::G), describe("h", units::constants::h), describe("mu0", units::constants::mu0), describe("epsilon0", units::constants::epsilon0), describe("Z0", units::constants::Z0), describe("k_e", units::constants::k_e), describe("e", units::constants::e), describe("m_e", units::constants::m_e), describe("m_p", units::constants::m_p), describe("mu_B", units::constants::mu_B), describe("N_A", units::constants::N_A), describe("R", units::constants::R), describe("k_B", units::constants::k_B), describe("F", units::constants::F), describe("sigma", units::constants::sigma), }; } static void print_constexpr() { constexpr std::array arr = describe_units(); static_assert(arr.size() > 15); // arr surely is a compile-time constant for (const auto& t : arr) { std::apply(&print, t); } } #endif // MYTEST_CONSTEXPR #if MYTEST_DYNAMIC template static void print_unit(const char* myname, const T& v) { print(myname, v.name(), v.abbreviation()); std::cout << "Regular cout: \"" << v << "\"\n"; } static void print_dynamic() { using namespace units::literals; const units::length::meter_t distance {1_m}; const units::angle::degree_t angle_deg {30_deg}; const units::angle::radian_t angle_rad {30_deg}; const units::dimensionless::scalar_t scalar {5}; print_unit("distance", distance); print_unit("angle_deg", angle_deg); print_unit("angle_rad", angle_rad); // print_unit("scalar", scalar); // print_unit("pi", units::constants::pi); print_unit("c", units::constants::c); // print_unit("G", units::constants::G); // print_unit("h", units::constants::h); // print_unit("mu0", units::constants::mu0); // print_unit("epsilon0", units::constants::epsilon0); print_unit("Z0", units::constants::Z0); // print_unit("k_e", units::constants::k_e); print_unit("e", units::constants::e); print_unit("m_e", units::constants::m_e); print_unit("m_p", units::constants::m_p); // print_unit("mu_B", units::constants::mu_B); // print_unit("N_A", units::constants::N_A); // print_unit("R", units::constants::R); // print_unit("k_B", units::constants::k_B); // print_unit("F", units::constants::F); // print_unit("sigma", units::constants::sigma); } #endif // MYTEST_DYNAMIC int main() { print_dynamic(); print_constexpr(); return 0; }