What is the best way to create a specialization-only function template?

前端 未结 2 1350
迷失自我
迷失自我 2021-02-14 19:38

Is there a better way to do the following?

#include 

template 
T Bar();

template <>
int Bar() { return 3; }
         


        
2条回答
  •  遇见更好的自我
    2021-02-14 19:57

    This could work:

    template 
    T Bar() {
      T::ERROR_invalid_template_argument_;
    }
    
    template <>
    int Bar() { return 3; }
    

    You could also use the highest size possible if you're afraid of using 0:

      static_assert(sizeof(T) == -1, "No specialization");
    

提交回复
热议问题