c++templatestypeinfo

Is there a way to get or define typeid of a template?


I have a template class which is dependent on multiple template class, like following:

#include <vector>
#include <string>
#include <typeinfo>
#include <iostream>

template <typename _T> 
struct Foo {
    _T mem;
};

template <typename _T> 
struct Bar {
    _T mem;
};

template <template <typename> typename ... _Dependencies>
struct Target {
    static std::vector<std::string> getSignature() {
        std::vector<std::string> vec{typeid(_Dependencies<int>).name()...};
        return vec;
    };
}; 

int main () {
    for (auto& item : Target<Foo, Bar>::getSignature()) {
        std::cout << item << std::endl;
    }; 
    return 0;
}

please notice this line:

std::vector<std::string> vec{typeid(_Dependencies<int>).name()...};

Here I have to give the template a specific type (e.g. int,std::string or sth else) to make the compile pass. However, what i want to get is not the instantiation template type, it is about the type of template class self, in order to distinguish between them.

Is it possible? or I have to use some instantiation type to achieve my aim ?


Solution

  • Templates are not types. Only types have typeids.

    template<template<class...>class>struct template_tag{};
    

    this is a template.

    template_tag<_Dependencies>
    

    this is a type.

    Take the typeid of template_tag<your template>.

    Aside:

    Note that a program where you name a type _Dependencies is ill formed no diagnostic required. Please stop mimicing std headers. They can name types things you are not allowed to.

    Never name anything a name starting with a _ followed by a capital letter. Never name anything something containing two __s. Both are reserved by the standard only to be used by compilers and std oibrary implementors.