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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:13:49+00:00 2026-05-17T06:13:49+00:00

I have read Answers to C++ interview questions among which there is one that

  • 0

I have read Answers to C++ interview questions among which there is one that puzzles me:

Q: When are temporary variables created by C++ compiler?

A: Provided that function parameter is a “const reference”, compiler generates temporary variable in following 2 ways.

a) The actual argument is the correct type, but it isn’t Lvalue

double Cube(const double & num)
{
  num = num * num * num;
  return num;
}

double temp = 2.0;
double value = cube(3.0 + temp); // argument is a expression and not a Lvalue

b) The actual argument is of the wrong type, but of a type that can be converted to the correct type

 long temp = 3L;
 double value = cuberoot(temp); // long to double conversion

My question is once the function argument is a const reference, why does the compiler generate the temporary variable, isn’t that self-contradictory? Also, should the function Cube fail to compile because it modifies the const argument?

  • 1 1 Answer
  • 1 View
  • 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-17T06:13:50+00:00Added an answer on May 17, 2026 at 6:13 am

    You are allowed to pass the results of an expression (including that of implicit casting) to a reference-to-const. The rationale is that while (const X & value) may be cheaper to use, depending on the copy-cost of type type X, than (X value), the effect is pretty much the same; value gets used but not modified (barring some dicey const-casting). Hence it is harmless to allow a temporary object to be created and passed to the function.

    You are not allowed to do so with pointer-to-const or reference-to-non-const, because unexpected (and bad) things can happen, such as you might expect the long temp to be cast back to long, which isn’t going to happen.

    You’re correct about num = num * num * num; being invalid. That’s a bug in the text, but the argument made by it holds.

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

Sidebar

Related Questions

I have read the various questions/answers here that basically indicate that having a clustered
Now that I have read an awfull lot of posts, articles, questions and answers
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have read some of the questions and answers here, but it none match
I have read many of the good questions and answers around multi-core programming how-tos
I have read similar questions and their answers, however, it seems none deal with
I have read many questions and answers, but I couldn´t find solution for my
I have read questions and answers about this topic but I still have some
I have read many questions/answers and tutorials and I still fail to implement hierarchical
I have read through all the threads out there that looked as if they

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.