Created
May 3, 2013 14:08
-
-
Save den-mentiei/5509320 to your computer and use it in GitHub Desktop.
Variadic template type list enumerating.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
template <typename... T> | |
struct counter; | |
template <> | |
struct counter<> { | |
static constexpr size_t count() { | |
return 0; | |
} | |
}; | |
template <typename Head, typename... Tail> | |
struct counter<Head, Tail...> { | |
static constexpr size_t count() { | |
return 1 + counter<Tail...>::count(); | |
} | |
}; | |
template <typename... T> | |
constexpr size_t count() { | |
return counter<T...>::count(); | |
} | |
int main() { | |
cout << count<int, bool, unsigned int, char>() << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment