fork(4) download
  1. #include <iostream>
  2.  
  3.  
  4. #include <ratio>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <ctgmath>
  8.  
  9.  
  10. namespace unit {
  11.  
  12. template <typename Unit, typename MagnitudeRepresentation = double>
  13. class Quantity {
  14. template <typename, typename>
  15. friend class Quantity;
  16.  
  17. public:
  18. using unit = Unit;
  19. using magnitude_type = MagnitudeRepresentation;
  20. using classtype = Quantity<unit, magnitude_type>;
  21.  
  22. constexpr explicit Quantity() : m_magnitude(0) {}
  23. constexpr explicit Quantity(magnitude_type const& v) : m_magnitude(v) {}
  24.  
  25. constexpr Quantity(classtype const& v) = default;
  26.  
  27. template <typename R>
  28. constexpr Quantity(Quantity<unit, R> const& v) : m_magnitude(v.m_magnitude) {}
  29.  
  30. classtype& operator=(classtype const& v) = default;
  31.  
  32. constexpr magnitude_type const& magnitude() const { return m_magnitude; }
  33.  
  34. private:
  35. template <typename U, typename L, typename R>
  36. friend constexpr Quantity<U, L>& operator+=(Quantity<U, L>&, Quantity<U, R> const&);
  37.  
  38. template <typename U, typename L, typename R>
  39. friend constexpr Quantity<U, L>& operator-=(Quantity<U, L>&, Quantity<U, R> const&);
  40.  
  41. template <typename U, typename L, typename R>
  42. friend constexpr Quantity<U, L>& operator*=(Quantity<U, L>&, Quantity<U, R> const&);
  43.  
  44. template <typename U, typename L, typename R>
  45. friend constexpr Quantity<U, L>& operator/=(Quantity<U, L>&, Quantity<U, R> const&);
  46.  
  47. magnitude_type m_magnitude;
  48. };
  49.  
  50. using TExponent = int;
  51. using ExponentIndex = int;
  52. using DimensionIndex = int;
  53.  
  54. template <TExponent... dimensionExponents>
  55. struct Unit;
  56.  
  57. namespace helper {
  58.  
  59.  
  60. template <typename Unit, ExponentIndex i>
  61. constexpr TExponent exponent() {
  62. return Unit::template exponent_value<i>();
  63. }
  64.  
  65. namespace nthElement {
  66.  
  67. using Index = unsigned int;
  68.  
  69. template <Index n, TExponent Arg, TExponent... Args>
  70. struct NthElement {
  71. static constexpr int value() { return NthElement<n - 1, Args...>::value(); }
  72. };
  73.  
  74. template <TExponent Arg, TExponent... Args>
  75. struct NthElement<0, Arg, Args...> {
  76. static constexpr int value() { return Arg; }
  77. };
  78. }
  79.  
  80. namespace baseunit {
  81.  
  82. template <DimensionIndex it, DimensionIndex index, TExponent... exponents>
  83. struct BaseUnitGen {
  84. using type = typename BaseUnitGen<it - 1, index, it == index ? 1 : 0, exponents...>::type;
  85. };
  86.  
  87. template <DimensionIndex index, TExponent... exponents>
  88. struct BaseUnitGen<-1, index, exponents...> {
  89. using type = Unit<exponents...>;
  90. };
  91. }
  92. }
  93.  
  94. template <int dimension, int lastDimension>
  95. using BaseUnit = typename helper::baseunit::BaseUnitGen<lastDimension, dimension>::type;
  96.  
  97. template<TExponent ... dimensionExponents >
  98. struct Unit{
  99.  
  100. using classtype = Unit<dimensionExponents...>;
  101.  
  102. static constexpr unsigned exponent_count() {return sizeof...(dimensionExponents);}
  103.  
  104. template<ExponentIndex i>
  105. static constexpr TExponent exponent_value() { return helper::nthElement::NthElement<i,dimensionExponents...>::value(); }
  106. };
  107.  
  108.  
  109. namespace helper {
  110.  
  111. namespace op {
  112.  
  113. template <typename U1, typename U2, int index>
  114. struct sum {
  115. static constexpr int value() { return U1::template exponent_value<index>() + U2::template exponent_value<index>(); }
  116. };
  117.  
  118. template <typename U1, typename U2, int index>
  119. struct difference {
  120. static constexpr int value() { return U1::template exponent_value<index>() - U2::template exponent_value<index>(); }
  121. };
  122.  
  123. template <typename U1, typename fract, int index>
  124. struct multiply {
  125. static_assert(helper::exponent<U1, index>() * fract::num % fract::den == 0,
  126. "rasing a unit to a fractional-power is not allowed");
  127. static constexpr int value() { return helper::exponent<U1, index>() * fract::num / fract::den; }
  128. };
  129.  
  130. template <ExponentIndex it, typename U1, typename U2, template <class, class, int> class Op, TExponent... exponents>
  131. struct UnitOperator {
  132. using type = typename UnitOperator<it - 1, U1, U2, Op, Op<U1, U2, it>::value(), exponents...>::type;
  133. };
  134.  
  135. template <typename U1, typename U2, template <class, class, int> class Op, TExponent... exponents>
  136. struct UnitOperator<-1, U1, U2, Op, exponents...> {
  137. using type = Unit<exponents...>;
  138. };
  139. }
  140. }
  141.  
  142. template <typename U1, typename U2>
  143. using product_unit = typename helper::op::UnitOperator<U1::exponent_count() - 1, U1, U2, helper::op::sum>::type;
  144.  
  145. template <typename U1, typename U2>
  146. using quotient_unit = typename helper::op::UnitOperator<U1::exponent_count() - 1, U1, U2, helper::op::difference>::type;
  147.  
  148. template <typename U1, typename fract>
  149. using raised_unit = typename helper::op::UnitOperator<U1::exponent_count() - 1, U1, fract, helper::op::multiply>::type;
  150.  
  151. template <typename U>
  152. using sqare_unit = product_unit<U, U>;
  153.  
  154.  
  155.  
  156.  
  157. template <typename TDst, typename QSrc>
  158. constexpr Quantity<typename QSrc::unit, TDst> static_unit_cast(QSrc const& l) {
  159. return Quantity<typename QSrc::unit, TDst>{static_cast<TDst>(l.magnitude())};
  160. }
  161.  
  162. template <typename U, typename LT, typename RT>
  163. constexpr Quantity<U, LT>& operator+=(Quantity<U, LT>& l, Quantity<U, RT> const& r) {
  164. return l.m_magnitude += r.m_magnitude, l;
  165. }
  166.  
  167. template <typename U, typename LT, typename RT>
  168. constexpr Quantity<U, LT>& operator-=(Quantity<U, LT>& l, Quantity<U, RT> const& r) {
  169. return l.m_magnitude -= r.m_magnitude, l;
  170. }
  171.  
  172. template <typename U, typename LT, typename RT>
  173. constexpr Quantity<U, LT>& operator*=(Quantity<U, LT>& l, Quantity<U, RT> const& r) {
  174. return l.m_magnitude *= r.m_magnitude, l;
  175. }
  176.  
  177. template <typename U, typename LT, typename RT>
  178. constexpr Quantity<U, LT>& operator/=(Quantity<U, LT>& l, Quantity<U, RT> const& r) {
  179. return l.m_magnitude /= r.m_magnitude, l;
  180. }
  181.  
  182. template <typename LU, typename LT, typename RU, typename RT>
  183. constexpr auto operator*(Quantity<LU, LT> const& l, Quantity<RU, RT> const& r)
  184. -> Quantity<product_unit<LU, RU>, decltype(l.magnitude() * r.magnitude())> {
  185. return Quantity<product_unit<LU, RU>, decltype(l.magnitude() * r.magnitude())>{l.magnitude() * r.magnitude()};
  186. }
  187.  
  188. template <typename LU, typename LT, typename RU, typename RT>
  189. constexpr auto operator/(Quantity<LU, LT> const& l, Quantity<RU, RT> const& r)
  190. -> Quantity<quotient_unit<LU, RU>, decltype(l.magnitude() / r.magnitude())> {
  191. return Quantity<quotient_unit<LU, RU>, decltype(l.magnitude() / r.magnitude())>{l.magnitude() / r.magnitude()};
  192. }
  193.  
  194. template <typename U, typename LT, typename RT>
  195. constexpr auto operator+(Quantity<U, LT> const& l, Quantity<U, RT> const& r)
  196. -> Quantity<U, decltype(l.magnitude() + r.magnitude())> {
  197. return Quantity<U, decltype(l.magnitude() + r.magnitude())>{l.magnitude() + r.magnitude()};
  198. }
  199.  
  200. template <typename U, typename LT, typename RT>
  201. constexpr auto operator-(Quantity<U, LT> const& l, Quantity<U, RT> const& r)
  202. -> Quantity<U, decltype(l.magnitude() - r.magnitude())> {
  203. return Quantity<U, decltype(l.magnitude() - r.magnitude())>{l.magnitude() - r.magnitude()};
  204. }
  205.  
  206. template <typename U, typename LT, typename RT>
  207. constexpr bool operator<(Quantity<U, LT> const& l, Quantity<U, RT> const& r) {
  208. return l.magnitude() < r.magnitude();
  209. }
  210.  
  211. template <typename U, typename LT, typename RT>
  212. constexpr bool operator<=(Quantity<U, LT> const& l, Quantity<U, RT> const& r) {
  213. return l.magnitude() <= r.magnitude();
  214. }
  215.  
  216. template <typename U, typename LT, typename RT>
  217. constexpr bool operator>(Quantity<U, LT> const& l, Quantity<U, RT> const& r) {
  218. return l.magnitude() > r.magnitude();
  219. }
  220.  
  221. template <typename U, typename LT, typename RT>
  222. constexpr bool operator>=(Quantity<U, LT> const& l, Quantity<U, RT> const& r) {
  223. return l.magnitude() >= r.magnitude();
  224. }
  225.  
  226. template <typename U, typename LT, typename RT>
  227. constexpr bool operator==(Quantity<U, LT> const& l, Quantity<U, RT> const& r) {
  228. return l.magnitude() == r.magnitude();
  229. }
  230.  
  231. template <typename U, typename LT, typename RT>
  232. constexpr bool operator!=(Quantity<U, LT> const& l, Quantity<U, RT> const& r) {
  233. return l.magnitude() != r.magnitude();
  234. }
  235.  
  236. template <typename LU, typename LT>
  237. constexpr Quantity<LU, LT> operator-(Quantity<LU, LT> const& l) {
  238. return Quantity<LU, LT>{-l.magnitude()};
  239. }
  240.  
  241.  
  242. template <typename U, typename T>
  243. constexpr auto
  244. abs(Quantity<U, T> const& q)
  245. -> Quantity<U, decltype(abs(q.magnitude()))>
  246. {
  247. using std::abs;
  248. return Quantity<U, decltype(abs(q.magnitude()))>{
  249. abs(q.magnitude())};
  250. }
  251.  
  252. template <typename U, typename T>
  253. constexpr auto
  254. sqrt(Quantity<U, T> const& q)
  255. -> Quantity<raised_unit<U, std::ratio<1, 2>>, decltype(sqrt(q.magnitude()))>
  256. {
  257. using std::sqrt;
  258. return Quantity<raised_unit<U, std::ratio<1, 2>>, decltype(sqrt(q.magnitude()))>{
  259. sqrt(q.magnitude())};
  260. }
  261.  
  262. namespace helper {
  263.  
  264. template <typename power, typename T>
  265. constexpr auto
  266. pow_impl(T const& v)
  267. -> decltype(pow(v, static_cast<double>(power::num) / static_cast<double>(power::den)))
  268.  
  269. {
  270. using std::pow;
  271. return pow(v, static_cast<double>(power::num) / static_cast<double>(power::den));
  272. }
  273.  
  274. }
  275.  
  276. template <typename power, typename U, typename T>
  277. constexpr auto
  278. pow(Quantity<U, T> const& q)
  279. -> Quantity<raised_unit<U, power>, decltype(helper::pow_impl<power, T>(q.magnitude()))>
  280. {
  281. return Quantity<raised_unit<U, power>, decltype(helper::pow_impl<power, T>(q.magnitude()))>{
  282. helper::pow_impl<power, T>(q.magnitude())};
  283. }
  284.  
  285. template <typename U, typename T>
  286. constexpr auto
  287. cube(Quantity<U, T> const& q)
  288. -> Quantity<product_unit<product_unit<U, U>, U>, decltype((q.magnitude() * q.magnitude()) * q.magnitude())>
  289. {
  290. return Quantity<product_unit<product_unit<U, U>, U>, decltype((q.magnitude() * q.magnitude()) * q.magnitude())>{
  291. (q.magnitude() * q.magnitude()) * q.magnitude()};
  292. }
  293.  
  294. template <typename U, typename T>
  295. constexpr auto
  296. square(Quantity<U, T> const& q)
  297. -> Quantity<product_unit<U, U>, decltype(q.magnitude() * q.magnitude())>
  298. {
  299. return Quantity<product_unit<U, U>, decltype(q.magnitude() * q.magnitude())>{
  300. q.magnitude() * q.magnitude()};
  301. }
  302.  
  303.  
  304.  
  305.  
  306.  
  307. template <typename U>
  308. constexpr const char* unitSymbol();
  309.  
  310. namespace helper {
  311.  
  312. namespace print {
  313.  
  314. template <typename U, DimensionIndex pos>
  315. void print_unit_symbol_and_exponent(std::ostream& s) {
  316. static constexpr TExponent e = helper::exponent<U, pos>();
  317. if (e == 0)
  318. return;
  319. s << unitSymbol<typename baseunit::BaseUnitGen<U::exponent_count() - 1, pos>::type>();
  320. if (e != 1) {
  321. s << "^" << e;
  322. }
  323. }
  324.  
  325. template <typename U, DimensionIndex pos>
  326. struct DimensionsPrinter {
  327. static void print_unit(std::ostream& s) {
  328. DimensionsPrinter<U, pos - 1>::print_unit(s);
  329. print_unit_symbol_and_exponent<U, pos>(s);
  330. }
  331. };
  332.  
  333. template <typename U>
  334. struct DimensionsPrinter<U, 0> {
  335. static void print_unit(std::ostream& s) { print_unit_symbol_and_exponent<U, 0>(s); }
  336. };
  337. }
  338. }
  339.  
  340. template <typename U>
  341. void print_unit(std::ostream& s) {
  342. helper::print::DimensionsPrinter<U, U::exponent_count() - 1>::print_unit(s);
  343. }
  344.  
  345. template <typename Unit>
  346. std::istream& read_unit(std::istream& s) {
  347. std::ostringstream expectedBuff;
  348. print_unit<Unit>(expectedBuff);
  349.  
  350. for (auto const& expected : expectedBuff.str()) {
  351. char found;
  352. s >> found;
  353. if (!s || found != expected) {
  354. s.setstate(std::ios::failbit);
  355. return s;
  356. }
  357. }
  358. return s;
  359. }
  360.  
  361. template <typename Unit, typename TValue>
  362. std::istream& operator>>(std::istream& s, Quantity<Unit, TValue>& v) {
  363. TValue val;
  364. s >> val;
  365. if (!s)
  366. return s;
  367.  
  368. if (!read_unit<Unit>(s))
  369. return s;
  370.  
  371. v = Quantity<Unit, TValue>{val};
  372. return s;
  373. }
  374.  
  375. template <typename Unit, typename TValue>
  376. std::ostream& operator<<(std::ostream& s, Quantity<Unit, TValue> const& v) {
  377. s << v.magnitude();
  378. print_unit<Unit>(s);
  379. return s;
  380. }
  381. namespace helper {
  382.  
  383. template <typename ratioIn, typename ratioOut, typename T>
  384. constexpr T rescale(T const& v) {
  385. using r = std::ratio_divide<ratioIn, ratioOut>;
  386. return v * T{r::num} / T{r::den};
  387. }
  388.  
  389. template <typename ratioIn, typename T>
  390. constexpr T rescaleTo1(T const& v) {
  391. return rescale<ratioIn, std::ratio<1>, T>(v);
  392. }
  393. }
  394.  
  395. template<typename T> constexpr T femto(T const& v) { return helper::rescaleTo1<std::femto>(v); }
  396. template<typename T> constexpr T pico(T const& v) { return helper::rescaleTo1<std::pico>(v); }
  397. template<typename T> constexpr T nano(T const& v) { return helper::rescaleTo1<std::nano>(v); }
  398. template<typename T> constexpr T micro(T const& v) { return helper::rescaleTo1<std::micro>(v); }
  399. template<typename T> constexpr T milli(T const& v) { return helper::rescaleTo1<std::milli>(v); }
  400. template<typename T> constexpr T centi(T const& v) { return helper::rescaleTo1<std::centi>(v); }
  401. template<typename T> constexpr T deci(T const& v) { return helper::rescaleTo1<std::deci>(v); }
  402. template<typename T> constexpr T deca(T const& v) { return helper::rescaleTo1<std::deca>(v); }
  403. template<typename T> constexpr T hecto(T const& v) { return helper::rescaleTo1<std::hecto>(v); }
  404. template<typename T> constexpr T kilo(T const& v) { return helper::rescaleTo1<std::kilo>(v); }
  405. template<typename T> constexpr T mega(T const& v) { return helper::rescaleTo1<std::mega>(v); }
  406. template<typename T> constexpr T giga(T const& v) { return helper::rescaleTo1<std::giga>(v); }
  407. template<typename T> constexpr T tera(T const& v) { return helper::rescaleTo1<std::tera>(v); }
  408. template<typename T> constexpr T peta(T const& v) { return helper::rescaleTo1<std::peta>(v); }
  409.  
  410.  
  411. namespace u {
  412.  
  413.  
  414.  
  415.  
  416. using unitless = BaseUnit<-1,6>;
  417.  
  418. using meter = BaseUnit<0,6>;
  419. using kilogram = BaseUnit<1,6>;
  420. using second = BaseUnit<2,6>;
  421. using ampere = BaseUnit<3,6>;
  422. using kelvin = BaseUnit<4,6>;
  423. using mole = BaseUnit<5,6>;
  424. using candela = BaseUnit<6,6>;
  425.  
  426.  
  427.  
  428.  
  429. using radian = unitless;
  430. using steradian = unitless;
  431. using hertz = quotient_unit< unitless, second>;
  432. using newton = quotient_unit< product_unit<kilogram, meter>, sqare_unit<second>>;
  433. using pascal = quotient_unit< newton, sqare_unit<meter>>;
  434. using joule = quotient_unit< newton, meter>;
  435. using watt = quotient_unit< joule, second>;
  436. using coulomb = product_unit< second, ampere>;
  437. using volt = quotient_unit< watt, ampere>;
  438. using farad = quotient_unit< coulomb, volt>;
  439. using ohm = quotient_unit< volt, ampere>;
  440. using siemens = quotient_unit< ampere, volt>;
  441. using weber = product_unit< volt, second>;
  442. using tesla = quotient_unit< weber, sqare_unit<meter>>;
  443. using henry = quotient_unit< weber, ampere>;
  444. using lumen = product_unit< candela, steradian>;
  445. using lux = quotient_unit< lumen, sqare_unit<meter>>;
  446. using becquerel = hertz;
  447. using gray = quotient_unit< joule, kilogram>;
  448. using sievert = gray;
  449. using katal = quotient_unit< mole, second>;
  450.  
  451.  
  452.  
  453. using meter_cubed = product_unit< meter, sqare_unit<meter>>;
  454. using meter_per_second = quotient_unit< meter, second>;
  455. using meter_per_second_squared = quotient_unit< meter, sqare_unit<second>>;
  456.  
  457. using mol_inv = quotient_unit< unitless, mole>;
  458.  
  459. }
  460.  
  461. namespace t {
  462.  
  463. using def = double;
  464.  
  465.  
  466. using unitless = Quantity<u::unitless, def>;
  467. using meter = Quantity<u::meter, def>;
  468. using kilogram = Quantity<u::kilogram, def>;
  469. using second = Quantity<u::second, def>;
  470. using ampere = Quantity<u::ampere, def>;
  471. using kelvin = Quantity<u::kelvin, def>;
  472. using mole = Quantity<u::mole, def>;
  473. using candela = Quantity<u::candela, def>;
  474.  
  475.  
  476. using radian = Quantity<u::radian, def>;
  477. using steradian = Quantity<u::steradian, def>;
  478. using hertz = Quantity<u::hertz, def>;
  479. using newton = Quantity<u::newton, def>;
  480. using pascal = Quantity<u::pascal, def>;
  481. using joule = Quantity<u::joule, def>;
  482. using watt = Quantity<u::watt, def>;
  483. using coulomb = Quantity<u::coulomb, def>;
  484. using volt = Quantity<u::volt, def>;
  485. using farad = Quantity<u::farad, def>;
  486. using ohm = Quantity<u::ohm, def>;
  487. using siemens = Quantity<u::siemens, def>;
  488. using weber = Quantity<u::weber, def>;
  489. using tesla = Quantity<u::tesla, def>;
  490. using henry = Quantity<u::henry, def>;
  491. using lumen = Quantity<u::lumen, def>;
  492. using lux = Quantity<u::lux, def>;
  493. using becquerel = Quantity<u::becquerel, def>;
  494. using gray = Quantity<u::gray, def>;
  495. using sievert = Quantity<u::sievert, def>;
  496. using katal = Quantity<u::katal, def>;
  497.  
  498.  
  499. using meter_cubed = Quantity<u::meter_cubed, def>;
  500. }
  501.  
  502.  
  503. constexpr t::unitless unitless{1};
  504. constexpr t::unitless number{1};
  505. constexpr t::meter meter{1};
  506. constexpr t::kilogram kilogram{1};
  507. constexpr t::second second{1};
  508. constexpr t::ampere ampere{1};
  509. constexpr t::kelvin kelvin{1};
  510. constexpr t::mole mole{1};
  511. constexpr t::candela candela{1};
  512.  
  513.  
  514. constexpr t::radian radian{1};
  515. constexpr t::steradian steradian{1};
  516. constexpr t::hertz hertz{1};
  517. constexpr t::newton newton{1};
  518. constexpr t::pascal pascal{1};
  519. constexpr t::joule joule{1};
  520. constexpr t::watt watt{1};
  521. constexpr t::coulomb coulomb{1};
  522. constexpr t::volt volt{1};
  523. constexpr t::farad farad{1};
  524. constexpr t::ohm ohm{1};
  525. constexpr t::siemens siemens{1};
  526. constexpr t::weber weber{1};
  527. constexpr t::tesla tesla{1};
  528. constexpr t::henry henry{1};
  529. constexpr t::lumen lumen{1};
  530. constexpr t::lux lux{1};
  531. constexpr t::becquerel becquerel{1};
  532. constexpr t::gray gray{1};
  533. constexpr t::sievert sievert{1};
  534. constexpr t::katal katal{1};
  535.  
  536.  
  537. constexpr t::meter inch{centi(2.54)};
  538. constexpr t::meter food{0.3048};
  539. constexpr t::meter mile{1609.344};
  540. constexpr t::meter yard{0.9144};
  541. constexpr t::kilogram pound{0.45359237};
  542. constexpr t::kilogram ounce{0.02834952};
  543. constexpr t::kilogram gram{helper::rescale<std::ratio<1>,std::kilo>(1.0)};
  544. constexpr t::second minute{60};
  545. constexpr t::second hour{60*60};
  546. constexpr t::second day{60*60*24};
  547. constexpr t::joule calorie{4.184};
  548. constexpr t::joule watt_hour{3600};
  549. constexpr t::meter_cubed liter{0.001};
  550. constexpr t::meter_cubed gallon{3.785412 * 0.001};
  551. constexpr t::pascal bar{kilo(100)};
  552. constexpr t::unitless parts_per_million{micro(1.0)};
  553. constexpr t::unitless percent{centi(1.0)};
  554.  
  555.  
  556.  
  557.  
  558.  
  559. constexpr t::kelvin celsius ( double v ){ return t::kelvin {static_cast<double>(v+273.15)};}
  560. constexpr t::kelvin fahrenheit ( double v ){ return t::kelvin {static_cast<double>((v+459.67)*5.0/9.0)};}
  561.  
  562.  
  563. namespace literals {
  564.  
  565. constexpr t::unitless operator"" _unitless ( long double v ) {return t::unitless {static_cast<double>(v)};}
  566. constexpr t::unitless operator"" _number ( long double v ) {return t::unitless {static_cast<double>(v)};}
  567. constexpr t::unitless operator"" _n ( long double v ) {return t::unitless {static_cast<double>(v)};}
  568. constexpr t::newton operator"" _newton ( long double v ) {return t::newton {static_cast<double>(v)};}
  569.  
  570. }
  571.  
  572.  
  573. template<typename U> constexpr const char* unitSymbol();
  574. template<> inline constexpr const char* unitSymbol< u::meter>(){ return "m";}
  575. template<> inline constexpr const char* unitSymbol<u::kilogram>(){ return "kg";}
  576. template<> inline constexpr const char* unitSymbol< u::second>(){ return "s";}
  577. template<> inline constexpr const char* unitSymbol< u::ampere>(){ return "A";}
  578. template<> inline constexpr const char* unitSymbol< u::kelvin>(){ return "K";}
  579. template<> inline constexpr const char* unitSymbol< u::mole>(){ return "mol";}
  580. template<> inline constexpr const char* unitSymbol< u::candela>(){ return "cd";}
  581.  
  582.  
  583.  
  584. template<typename U> void print_unit(std::ostream& s);
  585.  
  586.  
  587. template<> inline void print_unit< u::hertz>(std::ostream& s){ s<<"Hz"; }
  588. template<> inline void print_unit< u::newton>(std::ostream& s){ s<<"N"; }
  589. template<> inline void print_unit< u::pascal>(std::ostream& s){ s<<"Pa"; }
  590. template<> inline void print_unit< u::joule>(std::ostream& s){ s<<"J"; }
  591. template<> inline void print_unit< u::watt>(std::ostream& s){ s<<"W"; }
  592. template<> inline void print_unit< u::coulomb>(std::ostream& s){ s<<"C"; }
  593. template<> inline void print_unit< u::volt>(std::ostream& s){ s<<"V"; }
  594. template<> inline void print_unit< u::farad>(std::ostream& s){ s<<"F"; }
  595. template<> inline void print_unit< u::ohm>(std::ostream& s){ s<<"Ohm"; }
  596. template<> inline void print_unit< u::siemens>(std::ostream& s){ s<<"S"; }
  597. template<> inline void print_unit< u::weber>(std::ostream& s){ s<<"Wb"; }
  598. template<> inline void print_unit< u::tesla>(std::ostream& s){ s<<"T"; }
  599. template<> inline void print_unit< u::henry>(std::ostream& s){ s<<"H"; }
  600. template<> inline void print_unit< u::lumen>(std::ostream& s){ s<<"lm"; }
  601. template<> inline void print_unit< u::lux>(std::ostream& s){ s<<"Lx"; }
  602.  
  603. template<> inline void print_unit< u::gray>(std::ostream& s){ s<<"Gy"; }
  604.  
  605. template<> inline void print_unit< u::katal>(std::ostream& s){ s<<"ka"; }
  606.  
  607. }
  608.  
  609.  
  610. int main() {
  611.  
  612. using namespace unit;
  613. using namespace unit::literals;
  614.  
  615. t::newton some_force = 2.0_n*meter * 3.0_n*kilogram / square(2.0_n*second);
  616.  
  617. std::cout<<some_force<<"\n"; //prints "1.5N"
  618.  
  619. return 0;
  620. }
Success #stdin #stdout 0s 15224KB
stdin
Standard input is empty
stdout
1.5N