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

  • Home
  • SEARCH
  • 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 559393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:14:00+00:00 2026-05-13T12:14:00+00:00

I have a templated function that operates on a template-type variable, and if the

  • 0

I have a templated function that operates on a template-type variable, and if the value is less than 0, sets it to 0. This works fine, but when my templated type is unsigned, I get a warning about how the comparison is always false. This obviously makes sense, but since its templated, I’d like it to be generic for all data types (signed and unsigned) and not issue the warning.

I’m using g++ on Linux, and I’m guessing there’s a way to suppress that particular warning via command line option to g++, but I’d still like to get the warning in other, non-templated, cases. I’m wondering if there’s some way, in the code, to prevent this, without having to write multiple versions of the function?

template < class T >
T trim(T &val)
{
  if (val < 0)
  {
    val = 0;
  }
  return (val);
}
int main()
{
  char cval = 5;
  unsigned char ucval = 5;

  cout << "Untrimmed: " << (int)cval;
  cval = trim(cval);
  cout << " Trimmed: " << (int)cval << endl;

  cout << "Untrimmed: " << (int)ucval;
  cval = trim(ucval);
  cout << " Trimmed: " << (int)ucval << endl;

 return (0);
}
  • 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-13T12:14:00+00:00Added an answer on May 13, 2026 at 12:14 pm
    #include <algorithm>
    
    template<class T>
    T& trim(T& val) {
      val = std::max(T(0), val);
      return val;
    }
    

    It’s not apparent from the question that passing by non-const reference is appropriate. You can change the above return nothing (void), pass by value and return by value, or pass by const& and return by value:

    template<class T>
    T trim(T const& val);
    
    // example use:
    value = trim(value); // likely the most clear solution
    

    Generalize a bit more, even though outside the scope of your question:

    template<class T>
    T constrain(T const& value, T const& lower, T const& upper) {
      // returns value if value within [lower, upper] (inclusive end points)
      // returns lower if value < lower
      // otherwise returns upper
      assert(lower <= upper); // precondition
      return std::min(std::max(value, lower), upper);
    }
    
    template<class T>
    T constrain_range(T const& value, T const& lower, T const& upper) {
      // returns value if value within [lower, upper) (exclusive upper)
      // returns lower if value < lower
      // otherwise returns upper - 1
      assert(lower < upper); // precondition
      if      (value <  lower) return lower;
      else if (value >= upper) return upper - 1;
      else                     return value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I figured it out by myself now: In case you… May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer I don't think it has an official name, it's just… May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer You would have to do it this way: $('option:contains("Red")', '#c')[0].selected… May 13, 2026 at 7:33 pm

Related Questions

I have a join function that operates on STL strings. I want to be
I'm getting an error I don't know how to fix so I wondering if
I have a class of interest (call it X). I have a std::list<X*> (call
I have a templated function fct that uses some complex data structure based on
Is it possible to establish a set of templated function pointers, without the hassle

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.