Skip to content

Instantly share code, notes, and snippets.

@telishev
Created January 3, 2016 12:09
Show Gist options
  • Save telishev/673609d651c0baab0b8c to your computer and use it in GitHub Desktop.
Save telishev/673609d651c0baab0b8c to your computer and use it in GitHub Desktop.
#include <utility>
#include <initializer_list>
#include <stddef.h>
using namespace std;
#define USE_RECURSION 0
#if USE_RECURSION
template <typename...>
struct count;
template <typename T>
struct count<T> {
constexpr static size_t value = 0;
};
template <typename T, typename Head, typename... Tail>
struct count<T, Head, Tail...> {
constexpr static size_t value =
(std::is_same<T, Head>::value ? 1 : 0) + count<T, Tail...>::value;
};
#else
template<typename T, typename... Ts>
constexpr size_t count_impl()
{
size_t count = 0;
auto x = {(count += std::is_same<T, Ts>::value)...};
(void)x;
return count;
}
template<typename T, typename... Ts>
struct count
{
static constexpr size_t value = count_impl<T, Ts...> ();
};
#endif
template<typename... Ts> struct void_t {};
template<typename T, typename... Ts>
constexpr size_t count_func (void_t<Ts...>) { return count<T, Ts...>::value; }
template<typename... Ts>
auto create_void_impl (Ts... t) { return void_t<Ts...> ();}
template<size_t...s>
auto create_void1 (index_sequence<s...>) { return create_void_impl ((s, 1)...); }
template<size_t s>
auto create_void () { return create_void1 (make_index_sequence<s> ()); }
int main ()
{
return count_func<int> (create_void<1200>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment