Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:07:47+00:00 2026-06-05T19:07:47+00:00

Is there a way to have a template specialization based on a range of

  • 0

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
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:07:50+00:00Added an answer on June 5, 2026 at 7:07 pm

    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;
    }; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to have a template inherit another template? I'm not using
Is there no way to have an inline script within a Handlebars template? <script
If you have a template class with a static variable, is there any way
Is there a way to see the compiler-instantiated code for a function template or
Is there any way to have a template (Java -> Editor -> Templates) in
Is there a way to have the compile deduce the template parameter automatically? template<class
in our company we have a template for creating PowerPoint-presentations. Is there a way
I have a template: {{#people}} <div style=background-color: **gray/white**;><span>{{name}}</span>: <span>{{title}}</span></div> {{/people}} Is there a way
Is there any way to have multiple templates, and tell jTemplate which one to
Is there any way to have a random string in a Django template? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.