Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #123756

Re: Move semantics with literals

From Andrey Tarasevich <noone@noone.net>
Newsgroups comp.lang.c++
Subject Re: Move semantics with literals
Date 2026-04-19 12:01 -0700
Organization A noiseless patient Spider
Message-ID <10s38pm$67tc$1@dont-email.me> (permalink)
References <10rlkk9$6hau$1@dont-email.me> <87jyu960xp.fsf@posteo.de> <10rlldg$6qiv$1@dont-email.me> <10s0e96$3c5jp$1@dont-email.me> <10s25go$1kloq$1@dont-email.me>

Show all headers | View raw


On Sun 4/19/2026 1:59 AM, boltar@caprica.universe wrote:
> On Sat, 18 Apr 2026 10:16:21 -0700
> Andrey Tarasevich <noone@noone.net> gabbled:
>> template-related chaff. They just intended to check whether you are at 
>> home with the very basics, e.g. whether you understand that
>>
>>   int &&r = 123;
>>
>> is already perfectly valid. But your concerns about "moving a literal" 
> 
> Yes , a literal, a value stored in the *read-only* program text area:
> 
> int main()
> {
>      int &&i = 123;
> }
> 
> gives
> 
>         movl    $123, -12(%rbp)
> 

The only kind of [built-in] literal in C++ that can serve as an lvalue 
(i.e. a target for a pointer or a reference) is _string_ literal. All 
other kinds of literals are _not_ lvalues. You cannot "reference" them 
regardless of what you try. So, the "read-onlyness" and "text-areaness" 
of your `123` should not have been a concern for you from the very 
beginning. Nobody's trying to grab anything from there.

If the code compiles (and it does), it should have been clear to anyone 
(if only by pure deduction) that the reference will refer to _something_ 
_else_, not to that "read-only text area".

And the answer to that have been in the language since the beginning of 
times. And have already mentioned it above. Since the beginning of times 
you could do this in C++

   const int &r = 123;

and for all means and purposes it was equivalent to

   const int __unnamed_const_int_123 = 123, &r = __unnamed_const_int_123;

An unnamed temporary object of type `int` was quietly created in storage 
and the reference was bound to it. So, that `123` referenced by the 
reference ends up in "regular" storage, not in your "read-only text area".

With the introduction of rvalue references in C++11 the above behavior 
was naturally extended to cover

   int &&rr = 123;

This is expectedly equivalent to

   int __unnamed_int_123 = 123, &&rr = (int &&) __unnamed_int_123;

Again, in this case an unnamed _modifiable_ temporary object of type 
`int` is created in modifiable storage and the reference is bound to it.

So, nothing magical happens. Nobody's trying to modify the unmodifiable 
here. Nobody's trying to move anything from your sacred "read-only text 
area". Moreover, there's nothing fundamentally new involved here 
associated with these newfangled `&&` references. It has all been in the 
languages since dinos roamed the Earth.

-- 
Best regards,
Andrey

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Move semantics with literals boltar@caprica.universe - 2026-04-14 14:57 +0000
  Re: Move semantics with literals Jonathan Lamothe <jonathan@jlamothe.net> - 2026-04-14 11:01 -0400
    Re: Move semantics with literals wij <wyniijj5@gmail.com> - 2026-04-14 23:04 +0800
    Re: Move semantics with literals Jonathan Lamothe <jonathan@jlamothe.net> - 2026-04-14 11:10 -0400
      Re: Move semantics with literals David Brown <david.brown@hesbynett.no> - 2026-04-14 17:26 +0200
      Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-14 18:25 +0200
        Re: Move semantics with literals Jonathan Lamothe <jonathan@jlamothe.net> - 2026-04-14 13:02 -0400
        Re: Move semantics with literals boltar@caprica.universe - 2026-04-15 08:18 +0000
          Re: Move semantics with literals Paavo Helde <eesnimi@osa.pri.ee> - 2026-04-15 12:50 +0300
          Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-15 14:24 +0200
            Re: Move semantics with literals boltar@caprica.universe - 2026-04-15 15:07 +0000
              Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-15 17:49 +0200
              Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-15 17:51 +0200
                Re: Move semantics with literals boltar@caprica.universe - 2026-04-16 08:29 +0000
                Re: Move semantics with literals scott@slp53.sl.home (Scott Lurndal) - 2026-04-16 14:41 +0000
                Re: Move semantics with literals Chris Ahlstrom <OFeem1987@teleworm.us> - 2026-04-18 06:41 -0400
              Re: Move semantics with literals Richard Damon <Richard@Damon-Family.org> - 2026-04-18 12:46 -0400
            Re: Move semantics with literals "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-04-15 23:08 -0700
              Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-16 09:02 +0200
                Re: Move semantics with literals "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-04-16 04:39 -0700
    Re: Move semantics with literals boltar@caprica.universe - 2026-04-14 15:10 +0000
      Re: Move semantics with literals Jonathan Lamothe <jonathan@jlamothe.net> - 2026-04-14 11:15 -0400
      Re: Move semantics with literals scott@slp53.sl.home (Scott Lurndal) - 2026-04-14 17:17 +0000
        Re: Move semantics with literals Jonathan Lamothe <jonathan@jlamothe.net> - 2026-04-14 13:36 -0400
          Re: Move semantics with literals boltar@caprica.universe - 2026-04-15 08:21 +0000
      Re: Move semantics with literals Andrey Tarasevich <noone@noone.net> - 2026-04-18 10:16 -0700
        Re: Move semantics with literals boltar@caprica.universe - 2026-04-19 08:59 +0000
          Re: Move semantics with literals boltar@caprica.universe - 2026-04-19 09:09 +0000
          Re: Move semantics with literals Andrey Tarasevich <noone@noone.net> - 2026-04-19 12:01 -0700
  Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-14 17:14 +0200
    Re: Move semantics with literals boltar@caprica.universe - 2026-04-14 15:23 +0000
      Re: Move semantics with literals Bonita Montero <Bonita.Montero@gmail.com> - 2026-04-14 18:23 +0200
  Re: Move semantics with literals Andrey Tarasevich <noone@noone.net> - 2026-04-18 09:27 -0700
    Re: Move semantics with literals Andrey Tarasevich <noone@noone.net> - 2026-04-18 09:46 -0700

csiph-web