Skip to content

Instantly share code, notes, and snippets.

@tonyyang-svail
Last active April 14, 2018 01:29
Show Gist options
  • Save tonyyang-svail/1884d2e1c7e114b80ae06a1e02c42e05 to your computer and use it in GitHub Desktop.
Save tonyyang-svail/1884d2e1c7e114b80ae06a1e02c42e05 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <tuple>
template <size_t I, bool at_end, typename... ARGS>
struct IterOverTypesImpl;
template <size_t I, typename... ARGS>
struct IterOverTypesImpl<I, false, ARGS...> {
void operator()() {
using T = typename std::tuple_element<I, std::tuple<ARGS...>>::type;
T().hello();
constexpr auto size = std::tuple_size<std::tuple<ARGS...>>::value;
IterOverTypesImpl<I + 1, I + 1 == size, ARGS...> func;
func();
}
};
template <size_t I, typename... ARGS>
struct IterOverTypesImpl<I, true, ARGS...> {
void operator()() {
std::cout << "end of iter" << std::endl;
}
};
template <typename... ARGS>
struct IterOverTypes {
void operator()() {
static_assert(sizeof...(ARGS) != 0,
"IterOverTypes should be invoked by at least one type");
IterOverTypesImpl<0, false, ARGS...> func;
func();
}
};
struct Foo {
void hello() {
std::cout << "Foo said hello" << std::endl;
}
};
struct Bar {
void hello() {
std::cout << "Bar said hello" << std::endl;
}
};
int main() {
IterOverTypes<Foo, Bar>()();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment