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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:35:38+00:00 2026-05-27T16:35:38+00:00

Naturally, this won’t compile: int &z = 3; // error: invalid initialization of non-const

  • 0

Naturally, this won’t compile:

int &z = 3; // error: invalid initialization of non-const reference ....

and this will compile:

const int &z = 3; // OK

Now, consider:

const int y = 3;
int && yrr = y; // const error (as you would expect)
int && yrr = move(y); // const error (as you would expect)

But these next lines do compile for me. I think it shouldn’t.

int && w = 3;
int && yrr = move(3);
void bar(int && x) {x = 10;}
bar(3);

Wouldn’t those last two lines allow the literal 3 to be modified? What is the difference between 3 and a const int? And finally, Is there any danger with ‘modifying’ literals?

(g++-4.6 (GCC) 4.6.2 with -std=gnu++0x -Wall -Wextra)

  • 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-27T16:35:39+00:00Added an answer on May 27, 2026 at 4:35 pm

    The rvalue reference to the literal 3:

    int && w = 3;
    

    is actually bound to a temporary that is the result of evaluating the expression 3. It’s not bound to some Platonic literal 3.

    (all the following standards references are from the March 2011 draft, n3242)

    3.10/1 “Lvalues and rvalues”

    The value of a literal such as 12, 7.3e5, or true is also a prvalue

    Then 8.5.3 “References” gives the rules for how a reference is bound falls through to the last case, which says:

    Otherwise, a temporary of type “cv1 T1” is created and initialized from the initializer expression using the rules for a non-reference copy-initialization (8.5). The reference is then bound to the temporary.

    and gives as one example something very close to what’s in your question:

    double&& rrd = 2; // rrd refers to temporary with value 2.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have some function returning a pointer, or NULL on error. Now I'd
This is a general question, so I won't name which site's API I am
If I use something like double to hold file sizes in bytes, this will
This code must naturally sort an array of strings and it gives a segfault.
I have something like this: #include <iostream> namespace N { typedef std::pair<int, double> MyPair;
It would seem natural to me that HTML would support this: <div id=comment1> <div
I wrote this post Music and Mathematics, finding the Natural and the Pentatonic scales.
This is for simulation. In particular, I'm trying to generate natural sounding words and
I have to mix some colors in a natural way. This means blue +
Can SQLite sort naturally? For example, CREATE TABLE animals ( id INTEGER NOT NULL

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.