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 7887537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:36:23+00:00 2026-06-03T05:36:23+00:00

I want to write a template that returns me the smallest signed integer type

  • 0

I want to write a template that returns me the smallest signed integer type that can represent a given number. This is my solution:

/**
 * Helper for IntTypeThatFits.
 * Template parameters indicate whether the given number fits into 8, 16 or 32
 * bits. If neither of them is true, it is assumed that it fits 64 bits.
 */
template <bool fits8, bool fits16, bool fits32>
struct IntTypeThatFitsHelper { };

// specializations for picking the right type
// these are all valid combinations of the flags
template<> struct IntTypeThatFitsHelper<true, true, true> { typedef int8_t Result; };
template<> struct IntTypeThatFitsHelper<false, true, true> { typedef int16_t Result; };
template<> struct IntTypeThatFitsHelper<false, false, true> { typedef int32_t Result; };
template<> struct IntTypeThatFitsHelper<false, false, false> { typedef int64_t Result; };

/// Finds the smallest integer type that can represent the given number.
template <int64_t n>
struct IntTypeThatFits
{
    typedef typename IntTypeThatFitsHelper<
        (n <= numeric_limits<int8_t>::max()) && (n >= numeric_limits<int8_t>::min()), 
        (n <= numeric_limits<int16_t>::max()) && (n >= numeric_limits<int16_t>::min()), 
        (n <= numeric_limits<int32_t>::max()) && (n >= numeric_limits<int32_t>::min())
    >::Result Result;
};

However, GCC does not accept this code. I get an error “comparison is always true due to limited range of data type [-Werror=type-limits]”. Why does that happen? n is a signed 64bit integer, and all of the comparisons may be true or false for different values of n, or am I overlooking something?

I will be glad for any help.

Edit: I should mention that I am using C++11.

  • 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-03T05:36:24+00:00Added an answer on June 3, 2026 at 5:36 am

    It’s an issue with gcc, warnings in templated code can be frustrating. You can either change the warning or use another approach.

    As you may know, templated code is analyzed twice:

    • once when first encountered (parsing)
    • once when instantiated for a given type/value

    The problem here is that at instantiation, the check is trivial (yes 65 fits into an int thank you), and the compiler fails to realize that this warning does not hold for all instantiations 🙁 It is very frustrating indeed for those of us who strive to have a warning-free compiling experience with warnings on.

    You have 3 possibilities:

    • deactivate this warning, or demote it to a non-error
    • use a pragma to selectively deactivate it for this code
    • rework the code in another format so that it does not trigger the warning any longer

    Note that sometimes the 3rd possibility involves a massive change and much more complicated solution. I advise against complicated one’s code just to get rid of clueless warnings.

    EDIT:

    One possible workaround:

    template <int64_t n>
    struct IntTypeThatFits {
        static int64_t const max8 = std::numeric_limits<int8_t>::max();
        static int64_t const min8 = std::numeric_limits<int8_t>::min();
    
        static int64_t const max16 = std::numeric_limits<int16_t>::max();
        static int64_t const min16 = std::numeric_limits<int16_t>::min();
    
        static int64_t const max32 = std::numeric_limits<int32_t>::max();
        static int64_t const min32 = std::numeric_limits<int32_t>::min();
    
        typedef typename IntTypeThatFitsHelper<
            (n <= max8 ) && (n >= min8 ), 
            (n <= max16) && (n >= min16), 
            (n <= max32) && (n >= min32)
        >::Result Result;
    };
    

    … by changing the type of the data used in the comparison, it should silence the compiler warning. I suppose explicit casting (int64_t(std::numeric_limits<int8_t>::max())) could work too, but I found this more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a template function that checks some Timestamp property (class inherits
I want to write a generic averaging algorithm. That is, for any type T
I want to write a function that creates a Vector (template), stores a couple
I want to write a database wrapper that can operate different types of databases
I want to write a script that disable all the submit buttons when the
I want to write a test that calls a remote server and validates the
I want to write a program that uses the GPL-licensed pandoc for Markdown processing.
Possible Duplicate: Obtaining const_iterator from iterator I want to write a metafunction which returns
I want to write a function that calls another function with its arguments. See
I want to partially specialize an existing template that I cannot change ( std::tr1::hash

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.