Skip to content

Instantly share code, notes, and snippets.

@unix-beard
Last active August 29, 2015 14:22
Show Gist options
  • Save unix-beard/0ab79bae789f2dec2293 to your computer and use it in GitHub Desktop.
Save unix-beard/0ab79bae789f2dec2293 to your computer and use it in GitHub Desktop.
Idea for a work-around, when C++ 11 features are not available
#include <iostream>
template <typename T, unsigned int N>
unsigned int countof(T const (&array)[N])
{
return N;
}
void f1() { std::cout << "Hello from " << __func__ << "\n"; }
void f2() { std::cout << "Hello from " << __func__ << "\n"; }
void f3() { std::cout << "Hello from " << __func__ << "\n"; }
void f4() { std::cout << "Hello from " << __func__ << "\n"; }
void f5() { std::cout << "Hello from " << __func__ << "\n"; }
void f6() { std::cout << "Hello from " << __func__ << "\n"; }
typedef void (*func)();
const func arr1[] = {
f1
};
const func arr2[] = {
f2,
f3
};
const func arr3[] = {
f4,
f5,
f6
};
template<class Array>
void call_func(const Array& arr)
{
for (int i = 0; i < countof(arr); i++)
arr[i]();
}
int main()
{
call_func(arr1);
call_func(arr2);
call_func(arr3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment