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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:45:53+00:00 2026-06-07T19:45:53+00:00

This question is related to Does this C++ static analysis rule make sense as

  • 0

This question is related to Does this C++ static analysis rule make sense as is?, but is somewhat different. I’ve now implemented a static analysis rule to find cases where a function returns its const reference parameter as a reference, such as

const X& f(const X& x) { return x; }

This is potentially dodgy when a temporary is bound to x, because the lifetime of the temporary will end before the return value of f can be bound to a reference in the caller. To put it another way, this would be problematic:

const X& r = f(X());

On running the rule, I’m finding an implementation of min in the standard library that looks like this:

template<typename _Tp>
inline const _Tp&
min(const _Tp& __a, const _Tp& __b)
{
  // concept requirements
  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  //return __b < __a ? __b : __a;
  if (__b < __a)
    return __b;
  return __a;
}

This clearly returns its const reference parameters as references, but the function is inline. Does this make a difference in terms of temporary lifetime, or is this genuinely a bit dodgy? The function is marked with the following comment, so it’s clearly intended to be callable on temporaries:

*  This is the simple classic generic implementation.  It will work on
*  temporary expressions, since they are only evaluated once, unlike a
*  preprocessor macro.
  • 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-07T19:45:55+00:00Added an answer on June 7, 2026 at 7:45 pm

    Whether a function is inline or not makes absolutely no difference in
    the lifetime of any temporaries. Binding the return value of a function
    like your f (or std::min) to a const reference with local scope is a
    sure way to end up with a dangling reference. But I don’t see where
    there’s any real danger, since it’s not something you would do in
    reasonable code, unless the reference is to something long lived in the
    first place, e.g.:

    T const& obj = std::min( myMap['x'], myMap['y'] );
    

    Except in such special cases, local variables will be values, not
    references.

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

Sidebar

Related Questions

My question is somewhat related to this one: How does a generic constraint prevent
This question is related to (but perhaps not quite the same as): Does Django
Background This question is related to Why does String.valueOf(null) throw a NullPointerException? Consider the
Related to this question Given this function: public static <S extends CharSequence> S foo(S
This question was previously asked Here , but not answered, And failed to find
This is somewhat related to another question that I've asked that I've pretty much
This is closely related to, but is not the same question as: Java -
[This question is related to but not the same as this one .] My
This question is related to: Does Nvidia Cuda warp Scheduler yield? However, my question
I apologize for this question length, but I think you'll all find it worth

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.