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


is there way have template specialization based on range of values instead of one? know following code not valid c++ code shows do. i'm writing code 8-bit machine, there difference in speed using ints , 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 }; 

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 compiler doesn't yet support part of c++11, there's equivalent in boost libraries.

then again, it's easy roll own (credit goes 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; };  

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -