Guest User

Untitled

a guest
Jan 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. template <typename T, unsigned int N>
  2. unsigned int size(T const (&)[N])
  3. {
  4. return N;
  5. }
  6.  
  7. template <int N>
  8. struct Factorial {
  9. enum { value = N * Factorial<N - 1>::value };
  10. };
  11.  
  12. template <>
  13. struct Factorial<0> {
  14. enum { value = 1 };
  15. };
  16.  
  17. // Factorial<4>::value == 24
  18. // Factorial<0>::value == 1
  19. const int x = Factorial<4>::value; // == 24
  20. const int y = Factorial<0>::value; // == 1
  21.  
  22. template <typename T, unsigned int N>
  23. void print_array(T const (&arr)[N]) // both T and N are deduced
  24. {
  25. std::cout << "[";
  26. for (unsigned int i = 0; i != N; ++i)
  27. {
  28. if (i != 0) { std::cout << ", ";
  29. std::cout << arr[i];
  30. }
  31. std::cout << "]";
  32. }
  33.  
  34. int main()
  35. {
  36. double x[] = { 1.5, -7.125, 0, std::sin(0.5) };
  37. print_array(x);
  38. }
  39.  
  40. template <int N>
  41. struct A
  42. {
  43. // Other fields.
  44. int data[N];
  45. };
Add Comment
Please, Sign In to add comment