Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #89531
| From | "Alf P. Steinbach" <alf.p.steinbach@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: reference to reference (Type &&) |
| Date | 2023-03-08 18:08 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <tuafep$10ei5$1@dont-email.me> (permalink) |
| References | (4 earlier) <tu7mus$fa1o$1@dont-email.me> <tu8cp3$ipi2$1@dont-email.me> <tu9k20$rojc$1@dont-email.me> <tu9vpr$tjam$1@dont-email.me> <tuacv2$vte5$1@dont-email.me> |
On 2023-03-08 5:26 PM, Muttley@dastardlyhq.com wrote:
> On Wed, 8 Mar 2023 14:41:30 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 08.03.2023 11:21 Muttley@dastardlyhq.com kirjutas:
>>> On Wed, 8 Mar 2023 00:10:43 +0200
>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>> 07.03.2023 17:58 Muttley@dastardlyhq.com kirjutas:
>>>>> On Tue, 7 Mar 2023 03:48:11 -0800 (PST)
>>>>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>>>>>> All C++ move semantics is is wrapping this idea in nice syntax.
>>>>>
>>>>> Nice syntax but leaving the danger as you say of the source in a
>> potentially
>>>>> unusable state.
>>>>
>>>> This exact danger is why we have rvalue references in the language, so
>>>> that dangerous bindings are mostly avoided. Either there is a genuine
>>>> rvalue whose final state would not be important anyway, or we use
>>>> std::move() to explicitly declare we do not care about the final state
>>>> of the object.
>>>
>>> Thats fine, until its used in a library and someone doesn't look at the
>>> code so don't realise the object they passed in is now invalid. Yes thats
>>> a fairly unlikely scenario but something that needs to be considered when
>>> using move. Will someone use your code who isn't aware of what you've done?
>>>
>>
>> Care to bring an example? I have hard time figuring out how one could
>> get a moved-away object via rvalue references inadvertently.
>>
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> void myfunc(string &s1)
> {
> string s2 = move(s1);
> }
>
>
> int main()
> {
> string s1 = "hello";
> myfunc(s1);
> cout << s1 << endl;
> return 0;
> }
Entirely safe code.
A move from any standard library object leaves the object in a
well-defined state.
The originally proposed rules didn't completely guarantee all desired
safety, but as I recall Scott Meyers posted in this very group about it,
and after some discussion reasonable compromise was reached. :)
- Alf
PS: You don't need the `return 0;`; that's the default for `main` in
both C and C++.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
reference to reference (Type &&) MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-03-05 17:14 +0100
Re: reference to reference (Type &&) Öö Tiib <ootiib@hot.ee> - 2023-03-05 22:24 -0800
Re: reference to reference (Type &&) Paavo Helde <eesnimi@osa.pri.ee> - 2023-03-06 12:53 +0200
Re: reference to reference (Type &&) James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-03-06 14:31 -0500
Re: reference to reference (Type &&) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-03-06 14:00 -0800
Re: reference to reference (Type &&) MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-03-06 23:22 +0100
Re: reference to reference (Type &&) James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-03-06 19:48 -0500
Re: reference to reference (Type &&) MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-03-07 12:19 +0100
Re: reference to reference (Type &&) Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-03-07 03:48 -0800
Re: reference to reference (Type &&) Öö Tiib <ootiib@hot.ee> - 2023-03-07 04:30 -0800
Re: reference to reference (Type &&) Muttley@dastardlyhq.com - 2023-03-07 15:58 +0000
Re: reference to reference (Type &&) Paavo Helde <eesnimi@osa.pri.ee> - 2023-03-08 00:10 +0200
Re: reference to reference (Type &&) Muttley@dastardlyhq.com - 2023-03-08 09:21 +0000
Re: reference to reference (Type &&) Paavo Helde <eesnimi@osa.pri.ee> - 2023-03-08 14:41 +0200
Re: reference to reference (Type &&) Muttley@dastardlyhq.com - 2023-03-08 16:26 +0000
Re: reference to reference (Type &&) "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-03-08 18:08 +0100
Re: reference to reference (Type &&) Paavo Helde <eesnimi@osa.pri.ee> - 2023-03-08 19:23 +0200
Re: reference to reference (Type &&) Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-10 07:58 +0100
Re: reference to reference (Type &&) MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-03-09 02:19 +0100
csiph-web