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

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

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

4条回答
  •  误落风尘
    2021-01-04 12:20

    In C++14, you can use template variables to detect if a type is a specialization:

    #include 
    
    template
    constexpr bool is_spec = false;
    
    template class T, typename... U>
    constexpr bool is_spec> = true;
    
    struct S {};
    template struct R {};
    
    int main() {
        static_assert(not is_spec, "!");
        static_assert(is_spec>, "!");
    }
    

    Note that it won't work if non-type parameters are involved (as an example template struct R {};).

提交回复
热议问题