I'm trying to write a little template function that returns sequential integer id's for templates passed to it. The following works for many templates, but not for templates that take non-type arguments (like std::array). Is there any way to make this work more generally? If not, what is my best alternative? (I'm tempted to just trust the caller to only pass template template args, and force them to include some kind of pointless default template args so it's a normal type T: ttypeid<vector<void>>()
, ttypeid<array<void, 0>>()
, etc.)
#include <atomic>
#include <vector>
#include <map>
#include <array>
#include <iostream>
using namespace std;
inline unsigned gen_id() {
static atomic<unsigned> id = 0;
return ++id;
}
template <template<typename ...> typename T>
inline unsigned ttypeid() {
static unsigned id = gen_id();h
return id;
}
int main() {
cout << ttypeid<vector>() << endl;
cout << ttypeid<map>() << endl;
// cout << ttypeid<array>() << endl;
//
// error: no matching function for call to
// ‘ttypeid<template<class _Tp, long unsigned int _Nm> struct std::array>()’
return 0;
}