Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T, unsigned int N>
- unsigned int size(T const (&)[N])
- {
- return N;
- }
- template <int N>
- struct Factorial {
- enum { value = N * Factorial<N - 1>::value };
- };
- template <>
- struct Factorial<0> {
- enum { value = 1 };
- };
- // Factorial<4>::value == 24
- // Factorial<0>::value == 1
- const int x = Factorial<4>::value; // == 24
- const int y = Factorial<0>::value; // == 1
- template <typename T, unsigned int N>
- void print_array(T const (&arr)[N]) // both T and N are deduced
- {
- std::cout << "[";
- for (unsigned int i = 0; i != N; ++i)
- {
- if (i != 0) { std::cout << ", ";
- std::cout << arr[i];
- }
- std::cout << "]";
- }
- int main()
- {
- double x[] = { 1.5, -7.125, 0, std::sin(0.5) };
- print_array(x);
- }
- template <int N>
- struct A
- {
- // Other fields.
- int data[N];
- };
Add Comment
Please, Sign In to add comment