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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:50:26+00:00 2026-05-19T00:50:26+00:00

I have a templated function for returning the number of digits in a number:

  • 0

I have a templated function for returning the number of digits in a number:

    template <typename R>
    static inline unsigned count(const R num)
    {
        if(num < 10)                    return 1;
        else if (num < 100)             return 2;
        else if (num < 1000)            return 3;
        else if (num < 10000)           return 4;
        else if (num < 100000)          return 5;
        else if (num < 1000000)         return 6;
        else if (num < 10000000)        return 7;
        else if (num < 100000000)       return 8;
        else if (num < 1000000000)      return 9;
        else if (num < 10000000000ULL)          return 10;
        else if (num < 100000000000ULL)         return 11;
        else if (num < 1000000000000ULL)        return 12;
        else if (num < 10000000000000ULL)       return 13;
        else if (num < 100000000000000ULL)      return 14;
        else if (num < 1000000000000000ULL)     return 15;
        else if (num < 10000000000000000ULL)    return 16;
        else if (num < 100000000000000000ULL)   return 17;
        else if (num < 1000000000000000000ULL)  return 18;
        else if (num < 10000000000000000000ULL) return 19;
        else                                    return 20;
    }

However when I compile (GCC) I get the following warning:

warning: comparison is always true due to limited range of data type

I understand why I get this repeatedly but I’m not sure how to suppress/avoid it.

Any thoughts?

  • 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-05-19T00:50:27+00:00Added an answer on May 19, 2026 at 12:50 am

    If you don’t care about user-defined integer types (and evidence suggests that you don’t care about negative values, either), just define one function which takes the largest type that you care about:

    inline unsigned count(unsigned long long num){
        if(num < 10)                    return 1;
        else if (num < 100)             return 2;
        // blah blah
        else return 20;
    }
    

    If you call it with a signed short or whatever, you won’t get any warnings about the implicit conversion, since it’s a widening.

    static_cast<unsigned>(log10(num)) + 1 is also worth profiling.

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

Sidebar

Related Questions

I have the following templated function... template< class T > T *create_object( lua_State *L
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Suppose I have a static function template template<int I> void ft() within a struct
I have written a function template and an explicitly specialized templated function which simply
Say I have a template function in namespace A. I also have another namespace
Let's assume we have a template function foo: template<class T> void foo(T arg) {
I have a template class with a variadic template member function that I am

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.