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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:47:38+00:00 2026-05-31T05:47:38+00:00

for example this function f defined like this : int f(int x){return x;} as

  • 0

for example this function f defined like this :

int f(int x){return x;}

as you know You cant assign a reference to this temporary int :

int& rf=f(2);// this will give an error

but if I redefined my function f like this:

int& f(int x){return x;}
f(2);// so now f(2) is a reference of x, which has been destroyed 

so my question is : how can the compiler not let you create a reference to a temporary which will be destroyed after the statment (int the 1st case). and on the other hand it lets you create a reference f(2) to x while compiler knows that this one will be destroyed after return.

  • 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-31T05:47:40+00:00Added an answer on May 31, 2026 at 5:47 am

    Returning a reference to a local is something that can be difficult or impossible for the compiler to detect. For example:

    int & f()
    {
        int x;
        extern int & g(int & x);
    
        // Does this return a reference to "x"?
        // The compiler has no way to tell.
        return g(x);
    }
    

    Even without calling external functions, it can still be difficult to analyse a complex program flow to tell whether the returned reference is to a local; rather than trying to define what counts as “simple enough” to diagnose, the standard doesn’t require a diagnostic – it just states that it gives undefined behaviour. A good compiler should give a warning, at least in simple cases.

    Binding a temporary to a non-const reference is something that the compiler can easily detect, and so the standard does require a diagnostic for that.

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

Sidebar

Related Questions

Example: /** * This function will determine whether or not one string starts with
It is valid JavaScript to write something like this: function example(x) { Here is
Take the following example plugin: (function($) { $.fn.alertOnClick = function(text) { return this.each(function(){ $(this).click(alert(text));
Ideally I want a function something like the following (this example doesn't compile): void
For example: (function() { var proxied = window.eval; window.eval = function() { return proxied.apply(this,
For example, is it possible to write code like this: int $x = 6;
This example is from php.net: <?php function Test() { static $a = 0; echo
For example, in this simple function where fun1 takes as input two numbers, adds
I've simplified this example as much as I can. I have a remote function:
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });

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.