fork download
  1. #include <cassert>
  2. #include <type_traits>
  3.  
  4. using std::is_same;
  5. using std::declval;
  6. using std::integral_constant;
  7. using std::true_type;
  8. using std::false_type;
  9. using std::enable_if;
  10.  
  11. template <bool B, typename T = void>
  12. using enable_if_t = typename enable_if<B, T>::type;
  13.  
  14. template <typename...> struct void_type { using type = void; };
  15. template <typename... Ts> using void_t = typename void_type<Ts...>::type;
  16.  
  17. #define TYPIFY(x) integral_constant<decltype((x)), (x)>
  18. #define DETYPIFY(x) x::value
  19.  
  20. // structure representing an array of types
  21. template <typename...> struct type_array {};
  22.  
  23.  
  24. /**** example starts here ****/
  25.  
  26.  
  27. template <typename FirstCoef, typename T>
  28. T combine(type_array<FirstCoef>, const T &value) {
  29. return DETYPIFY(FirstCoef) * value;
  30. }
  31.  
  32. template <typename FirstCoef, typename... OtherCoefs,
  33. typename T, typename... Ts>
  34. T combine(
  35. type_array<FirstCoef, OtherCoefs...>, // dummy parameter
  36. const T &value, const Ts &... other_values) {
  37. return DETYPIFY(FirstCoef) * value +
  38. combine(type_array<OtherCoefs...>{}, other_values...);
  39. }
  40.  
  41.  
  42. int main() {
  43. using coefs = type_array<TYPIFY(5), TYPIFY(2), TYPIFY(3)>;
  44. assert(combine(coefs{}, 1, 3, 2) == 5 + 3 * 2 + 3 * 2);
  45. }
Success #stdin #stdout 0s 4340KB
stdin
Standard input is empty
stdout
Standard output is empty