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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:03:22+00:00 2026-05-15T11:03:22+00:00

The min algorithm is normally expressed like this: template <typename T> const T& min(const

  • 0

The min algorithm is normally expressed like this:

template <typename T>
const T& min(const T& x, const T& y)
{
    return y < x ? y : x;
}

However, this does not allow constructs of the form min(a, b) = 0. You can achieve that with an additional overload:

template <typename T>
T& min(T& x, T& y)
{
    return y < x ? y : x;
}

What I would like to do is unify these two overloads via perfect forwarding:

template <typename T>
T&& min(T&& x, T&& y)
{
    return y < x ? std::forward<T>(y) : std::forward<T>(x);
}

However, g++ 4.5.0 spits out a warning for min(2, 4) that I return a reference to a temporary. Did I do something wrong?


Okay, I get it. The problem is with the conditional operator. In my first solution, if I call min(2, 4) the conditional operator sees an xvalue and thus moves from the forwarded x to produce a temporary object. Of course it would be dangerous to return that by reference! If I forward the whole expression instead of x and y seperately, the compiler does not complain anymore:

template <typename T>
T&& min(T&& x, T&& y)
{
    return std::forward<T>(y < x ? y : x);
}

Okay, I got rid of the references for arithmetic types 🙂

#include <type_traits>

template <typename T>
typename std::enable_if<std::is_arithmetic<T>::value, T>::type
min(T x, T y)
{
    return y < x ? y : x;
}

template <typename T>
typename std::enable_if<!std::is_arithmetic<T>::value, T&&>::type
min(T&& x, T&& y)
{
    return std::forward<T>(y < x ? y : x);
}
  • 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-15T11:03:23+00:00Added an answer on May 15, 2026 at 11:03 am

    It looks to me like you’re trying to oversimplify the problem. Unfortunately, getting it entirely correct is decidedly non-trivial. If you haven’t read N2199, now would be a good time to do so. Rvalue references continue to evolve, so its reference implementation of min and max probably isn’t exactly right anymore, but it should at least be a pretty decent starting point. Warning: the reference implementation is a lot more complex than you’re going to like!

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

Sidebar

Related Questions

Does min-height not work on body/html? body, html { min-height:100%; } Accomplishes absolutely nothing
The code is return min + static_cast<int>(static_cast<double>(max - min + 1.0) * (number /
Does hibernate HQL queries support using select min, max, count and other sql functions?
Does anybody knows how can I get the max and min value of the
I'm looking for an algorithm to find bounding box (max/min points) of a closed
I'm using Python's max and min functions on lists for a minimax algorithm, and
I tried setting min-height property of <td style=min-height:500px></td> Works fine on ie8, but not
I have a layout which uses min-width and works great. So I came to
Any web service to get the monthly min/max temperatures for cities over a period
How do I set Java's min and max heap size through environment variables? 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.