Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

templates

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 like to do. I'm writing code for a 8-bit machine, so there is a difference in speed for using ints and chars.

template<unsigned SIZE> class circular_buffer {    unsigned char buffer[SIZE];    unsigned int head; // index    unsigned int tail; // index };  template<unsigned SIZE <= 256> class circular_buffer {    unsigned char buffer[SIZE];    unsigned char head; // index    unsigned char tail; // index }; 
like image 568
Mike Avatar asked Jun 13 '12 16:06

Mike


1 Answers

Try std::conditional:

#include <type_traits>  template<unsigned SIZE> class circular_buffer {      typedef typename         std::conditional< SIZE < 256,                           unsigned char,                           unsigned int                         >::type         index_type;      unsigned char buffer[SIZE];     index_type head;     index_type tail; }; 

If your compiler doesn't yet support this part of C++11, there's equivalent in boost libraries.

Then again, it's easy to roll your own (credit goes to KerrekSB):

template <bool, typename T, typename F> struct conditional {     typedef T type; };  template <typename T, typename F>  // partial specialization on first argument struct conditional<false, T, F> {     typedef F type; };  
like image 177
jrok Avatar answered Sep 25 '22 21:09

jrok