Skip to content

Instantly share code, notes, and snippets.

@enobayram
Created December 21, 2016 12:20
Show Gist options
  • Save enobayram/22fc07ac8ed176197f063b88ab12116c to your computer and use it in GitHub Desktop.
Save enobayram/22fc07ac8ed176197f063b88ab12116c to your computer and use it in GitHub Desktop.
// We create a global char for each type
// But we actually care about its address to be used as a constexpr token
template <class T> char token;
using type_token = const void *;
// A utility to get a list of tokens for a list of types
template <class ... Elems> constexpr type_token tokens[sizeof...(Elems)] = {&token<Elems>...};
// A utility template to pass around a list of types as a value
template <class ... T> struct list{};
// A constexpr function that finds the index of a type in a list of types
template <class T, class ... Ts> constexpr int find(list<Ts...>) {
int result = -1;
for(int i=0; i<sizeof...(Ts); ++i) if(tokens<Ts...>[i] == &token<T>) return i;
return result;
}
template <int N> int f() {return N;}
int phony = f<find<int>(list<double, int>{})>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment