fork download
  1. #include <iostream>
  2.  
  3. template< typename T, T... VALUES > struct seq_c {};
  4.  
  5. template< typename T, T... > struct size ;
  6. template< typename T, T... VALUES > struct size< seq_c< T, VALUES...> >
  7. { enum { value = sizeof...(VALUES) } ; } ;
  8.  
  9. template< typename T, T V, typename U > struct push_front {} ;
  10. template< typename T, T V, T... VALUES > struct push_front< T, V, seq_c< T, VALUES...> >
  11. { typedef seq_c< T, V, VALUES... > type ; } ;
  12.  
  13. template< typename T > struct pop_front {} ;
  14. template< typename T, T V, T... VALUES > struct pop_front< seq_c< T, V, VALUES...> >
  15. { typedef seq_c< T, VALUES...> type ; } ;
  16.  
  17.  
  18. template< typename T > struct sum ;
  19. template< typename T > struct sum < seq_c<T> > { static constexpr T value = T() ; } ;
  20. template< typename T, T V, T... VALUES > struct sum< seq_c< T, V, VALUES...> >
  21. { static constexpr T value = V + sum< seq_c< T, VALUES... > >::value ; } ;
  22.  
  23. // .. etc
  24.  
  25. int main()
  26. {
  27. using ten_to_fifteen = seq_c< int, 10, 11, 12, 13, 14, 15 > ;
  28. std::cout << "size of ten_to_fifteen: " << size<ten_to_fifteen>::value
  29. << " sum: " << sum<ten_to_fifteen>::value << '\n' ;
  30.  
  31. using nine_to_fifteen = push_front< int, 9, ten_to_fifteen >::type ;
  32. std::cout << "size of nine_to_fifteen: " << size<nine_to_fifteen>::value
  33. << " sum: " << sum<nine_to_fifteen>::value << '\n' ;
  34.  
  35. using eleven_to_fifteen = pop_front< ten_to_fifteen >::type ;
  36. std::cout << "size of eleven_to_fifteen: " << size<eleven_to_fifteen>::value
  37. << " sum: " << sum<eleven_to_fifteen>::value << '\n' ;
  38. }
  39.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
size of ten_to_fifteen: 6    sum: 75
size of nine_to_fifteen: 7    sum: 84
size of eleven_to_fifteen: 5    sum: 65