Disallow a specific function template instantiation

后端 未结 4 459
醉梦人生
醉梦人生 2021-02-05 23:59

I would like to define a template function but disallow instantiation with a particular type. Note that in general all types are allowed and the generic template works, I just w

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-06 00:14

    If you don't want to rely on static_assert or make the code portable pre-C++0x, use this:

    template
    void func(){
        typedef char ERROR_in_the_matrix[std::is_same::value? -1 : 1];
    }
    
    int main(){
      func(); // no error
      func(); // error: negative subscript
    }
    

提交回复
热议问题