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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:03:16+00:00 2026-06-04T07:03:16+00:00

This came up at work today, so I thought I would throw it out

  • 0

This came up at work today, so I thought I would throw it out to the community.

A co-worker wrote the following code (more or less):

#include <algorithm>

double chop(double x) {
    return std::max(0, x);
}

But this does not even compile, because std::max wants both of its arguments to have the exact same type. I believe this is so it can take a pair of references and return a reference, which is very likely what you want if you invoke it on a user-defined type. Fair enough.

The fix, of course, is to use std::max(0.0, x).

Now bear with me for a moment, because I lied. What my coworker actually wrote was this:

// Included from a very old header file written by someone long gone
template<class T1, class T2>
inline T1 myMax(T1 x, T2 y) {
    return (x < y) ? y : x;
}

double chop(double x) {
    return myMax(0, x);
}

This compiles! But it produces rather surprising results for x equal to, say, 0.25. I am not sure how long it took him to find the problem, and even after finding it, he had to ask why it was not working.

My answer was (a) Use 0.0 instead of 0 (which fixes the bug), and (b) use std::max instead of myMax (whose behavior is quite frightening when you think about it).

But he is wondering why that has to be. I mean, he can write 0 + x or 0 * x or 0 - x, so why not myMax(0, x)?

Here is my first pass at giving him what he wants:

// this is from the .hh file

// template meta-program to compute the "wider" of two types given as argument
template<class T1, class T2>
struct WiderType {
};

// Partial specialization for case where both types are same
template<class T>
struct WiderType<T, T> {
  typedef T type;
};

// Specialization for first type "int" and second type "double"
template<>
struct WiderType<int, double> {
  typedef double type;
};

template<class T1, class T2>
inline typename WiderType<T1,T2>::type
myMax(T1 a, T2 b) {
  return ((a < b) ? b : a);
}


// this is from the .cc file

double chop(double x) {
  return myMax(0, x);
}

// just to show this still works
int chop(int x) {
  return myMax(0, x);
}

Now, I could go through and add a specialization for WiderType for every pair of integral types, plus some to do the other Usual Arithmetic Conversions. (And I guess I could rename it UsualConversions or somesuch.)

But is there an easier way? That is, does the C++ language give me a simple way to define my own function that performs the same conversions on its arguments as the various built-in arithmetic operators?

  • 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-04T07:03:17+00:00Added an answer on June 4, 2026 at 7:03 am

    In addition to Charles Bailey’s answer, you can do this as well:

    template<typename T1, typename T2>
    typename std::common_type<T1, T2>::type max(T1&& a, T2&& b) {
        return a < b ? b : a;
    }
    

    common_type has a typedef type in it that is the type that both types can be implicitly converted to, so if it were, for instance, double and int, it would return a double, but if it were int and int, it would return an int.

    If you can’t use C++11 at all, then the only thing I can think of is this:

    template<typename T1, typename T2, typename T3>
    void max(const T1& a, const T2& b, T3& dst) {
        dst = a < b ? b : a;
    }
    

    and use it like

    double d;
    max(0, 43.12, d);
    

    It’s pretty clumsy having to declare a variable that way though. If you think it’s prettier, you can also do this:

    template<typename RetType, typename T1, typename T2>
    RetType max(const T1& a, const T2& b) {
        return a < b ? b : a;
    }
    

    and then

    return max<double>(0, 43.11);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem This question actually came up at work today. We are planning an experiment
Came into the office today to do some more work on my nearly completed
I came to work today and i found my hudson with this problem! I've
Today, during some other somehow related work, came across my mind the following question:
I came across this piece of code today while tutoring some students in a
I don't have much experience with QT and this problem came out today. QList<int>
I just came up with this, it seems to work in all modern browsers,
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
I tried to compile Ercia Sadun's sample code here , but this error came
I tried grep -v '^$' in Linux and that didn't work. This file came

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.