Is it possible to check for existence of member templates just by an identifier?

后端 未结 4 985
醉酒成梦
醉酒成梦 2021-01-04 11:54

Can we detect member function template, variable template, class/struct/union template or alias templ

4条回答
  •  梦毁少年i
    2021-01-04 12:09

    I can show you how to detect a struct template:

    template < class > struct check_template : std::false_type {};
    
    // Specialize for template classes
    template  class X, class... Args>
    struct check_template< X > : std::true_type {};
    

    You can then probably play around with declval, void_t, etc. to detect member templates.

    In case you want to detect types vs. meta-types, i.e. templates like std::vector and not std::vector, you can do the following:

    #include 
    
    template  class>
    constexpr bool is_template()
    {
        return true;
    }
    
    template 
    constexpr bool is_template()
    {
        return false;
    }
    
    struct Foo{};
    
    template
    struct TemplateFoo{};
    
    int main()
    {
         std::cout << std::boolalpha;
         std::cout << is_template() << std::endl;
         std::cout << is_template() << std::endl;
    }
    

    Live on Coliru

    Note that the solutions won't work if the meta-type has any non-type parameters, like

    template struct X{};
    

提交回复
热议问题