fork download
  1. #include <type_traits>
  2.  
  3. using REAL = double;
  4.  
  5. template <int P>
  6. constexpr typename std::enable_if<P == 0, REAL>::type
  7. const_pow(REAL ) { return 1.0; }
  8.  
  9. template <int P>
  10. constexpr typename std::enable_if<P == 1, REAL>::type
  11. const_pow(REAL value) { return value; }
  12.  
  13. template <int P>
  14. constexpr typename std::enable_if<P == 2, REAL>::type
  15. const_pow( REAL value ) { return value * value; }
  16.  
  17. template <int P>
  18. constexpr typename std::enable_if<2 < P, REAL>::type
  19. const_pow(REAL value)
  20. {
  21. return const_pow<2>(const_pow<P / 2>(value)) * const_pow<P % 2>(value);
  22. }
  23.  
  24. template <int P>
  25. constexpr typename std::enable_if<P < 0, REAL>::type
  26. const_pow(REAL value) { return const_pow<-P>(1.0 / value); }
  27.  
  28.  
  29. static_assert(0.5 == const_pow<-1>(2.), "Unepected result");
  30. static_assert(1. == const_pow<0>(2.), "Unepected result");
  31. static_assert(2. == const_pow<1>(2.), "Unepected result");
  32. static_assert(4 == const_pow<2>(2.), "Unepected result");
  33. static_assert(8 == const_pow<3>(2.), "Unepected result");
  34. static_assert(2048 == const_pow<11>(2.), "Unepected result");
  35.  
  36.  
  37. int main()
  38. {
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty