Template specialization for enum

后端 未结 3 1995
星月不相逢
星月不相逢 2020-12-17 23:19

Is it possible to specialize a templatized method for enums?

Something like (the invalid code below):

template 
void f(T value);

t         


        
3条回答
  •  伪装坚强ぢ
    2020-12-17 23:46

    I'm not sure if I understand your question correctly, but you can instantiate the template on specific enums:

    template 
    void f(T value);
    
    enum cars { ford, volvo, saab, subaru, toyota };
    enum colors { red, black, green, blue };
    
    template <>
    void f(cars) { }
    
    template <>
    void f(colors) { }
    
    int main() {
        f(ford);
        f(red);
    }
    

提交回复
热议问题