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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:18:25+00:00 2026-06-11T09:18:25+00:00

Error! $ cat double.cc #include<iostream> int main() { int i = 42; double &r

  • 0

Error!

$ cat double.cc
#include<iostream>

int main() {
    int i = 42;
    double &r = i;
}
$ g++ double.cc
double.cc: In function ‘int main()’:
double.cc:5: error: invalid initialization of reference of type ‘double&’
from expression of type ‘int’
$

Works!

$ cat double.cc
#include<iostream>

int main() {
    int i = 42;
    const double &r = i;
}
$ g++ double.cc
$

Why do we get the error in the first case? I know this is discussed here, but I am not sure I understand why this not allowed, while int to const double& is allowed?

  • 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-11T09:18:26+00:00Added an answer on June 11, 2026 at 9:18 am

    When you write

    int i = 42;
    double const &r = i;
    

    i is implicitly converted to a double, which results in a temporary value, or rvalue. A non-const reference is not allowed to bind to an rvalue, so r needs to be a double const& for the above to work.

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

Sidebar

Related Questions

This coding i got from here. I get error in log cat view like
$ cat -n cons.cpp 1 #include <iostream> 2 3 using namespace std; 4 5
Error : Error 1 bin\Debug\Daemon.exe.manifest;bin\Release\Daemon.exe.manifest is an invalid value for the InputManifest parameter of
Error: Unable to read data from the transport connection: A blocking operation was interrupted
Error I'm receiving Invalid argument supplied for foreach() The offending portions is this: foreach($subs[$id]
I have a command like this : cat error | grep -o [0-9] which
The script pasted below causes the following error: cat: can't open '/tmp/drive/P0.RAW': No such
How do I fwrite a struct containing an array #include <iostream> #include <cstdio> typedef
How can I invoke a shell command from python which includes regexp (i.e., cat
I am trying to generate some numbers with the rand() function like below: int

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.