How can I specialize a C++ template for a range of integer values?

后端 未结 4 846
离开以前
离开以前 2020-12-07 22:51

Is there a way to have a template specialization based on a range of values instead of just one? I know the following code is not valid C++ code but it shows what I would li

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 23:20

    Use an extra defaulted bool parameter:

    // primary template handles false
    template
    class circular_buffer {
       unsigned char buffer[SIZE];
       unsigned int head; // index
       unsigned int tail; // index
    };
    
    // specialization for true
    template
    class circular_buffer {
       unsigned char buffer[SIZE];
       unsigned char head; // index
       unsigned char tail; // index
    };
    

提交回复
热议问题