Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85461 > unrolled thread
| Started by | Muttley@dastardlyhq.com |
|---|---|
| First post | 2022-07-18 15:55 +0000 |
| Last post | 2022-07-19 07:24 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.c++
Do any STL inserters use std::move() by default? Muttley@dastardlyhq.com - 2022-07-18 15:55 +0000
Re: Do any STL inserters use std::move() by default? Gawr Gura <gawrgura@mail.hololive.com> - 2022-07-18 10:25 -0700
Re: Do any STL inserters use std::move() by default? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-07-18 20:36 +0200
Re: Do any STL inserters use std::move() by default? Manfred <noname@add.invalid> - 2022-07-19 02:30 +0200
Re: Do any STL inserters use std::move() by default? Juha Nieminen <nospam@thanks.invalid> - 2022-07-19 07:24 +0000
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2022-07-18 15:55 +0000 |
| Subject | Do any STL inserters use std::move() by default? |
| Message-ID | <tb3vor$1foi$1@gioia.aioe.org> |
Having to to call move() manually eg: v.push_back(std::move(str)) when you need move functionality can become tedious. Are there any or will there be any inserters that use move() by default?
[toc] | [next] | [standalone]
| From | Gawr Gura <gawrgura@mail.hololive.com> |
|---|---|
| Date | 2022-07-18 10:25 -0700 |
| Message-ID | <tb452f$fp7g$1@dont-email.me> |
| In reply to | #85461 |
On 7/18/22 08:55, Muttley@dastardlyhq.com wrote:
> Having to to call move() manually eg: v.push_back(std::move(str)) when you
> need move functionality can become tedious. Are there any or will there be any
> inserters that use move() by default?
>
You could insert using a std::move_iterator but this might not be as
simple as you want. You could also write your own wrapper over
std::vector::push_back to get a version that always moves:
template <typename Type>
void move_back(std::vector<std::remove_reference_t<Type>>& v, Type&& t) {
v.push_back(std::move(t));
}
I could be wrong but I don't think there's a way to force a correct
compiler to select an rvalue-reference overload like this without
std::move or something equivalent to std::move. The exception would be a
case where the input is not an lvalue yet
(e.g., v.push_back(std::string{ "Hello" })). It seems to me that, in any
case, this would likely just be extra typing to achieve the exact same
result.
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach@gmail.com> |
|---|---|
| Date | 2022-07-18 20:36 +0200 |
| Message-ID | <tb496u$gphh$1@dont-email.me> |
| In reply to | #85462 |
On 18 Jul 2022 19:25, Gawr Gura wrote:
>
> On 7/18/22 08:55, Muttley@dastardlyhq.com wrote:
>> Having to to call move() manually eg: v.push_back(std::move(str)) when
>> you
>> need move functionality can become tedious. Are there any or will
>> there be any
>> inserters that use move() by default?
>>
>
> You could insert using a std::move_iterator but this might not be as
> simple as you want. You could also write your own wrapper over
> std::vector::push_back to get a version that always moves:
>
> template <typename Type>
> void move_back(std::vector<std::remove_reference_t<Type>>& v, Type&& t) {
> v.push_back(std::move(t));
> }
>
> I could be wrong but I don't think there's a way to force a correct
> compiler to select an rvalue-reference overload like this without
> std::move or something equivalent to std::move. The exception would be a
> case where the input is not an lvalue yet
> (e.g., v.push_back(std::string{ "Hello" })). It seems to me that, in any
> case, this would likely just be extra typing to achieve the exact same
> result.
I almost didn't recognize that that's a universal reference for the `t`
argument.
What a language.
But it's a hate/love-relationship.
- Alf
[toc] | [prev] | [next] | [standalone]
| From | Manfred <noname@add.invalid> |
|---|---|
| Date | 2022-07-19 02:30 +0200 |
| Message-ID | <tb4u02$1ee2$1@gioia.aioe.org> |
| In reply to | #85461 |
On 7/18/2022 5:55 PM, Muttley@dastardlyhq.com wrote: > Having to to call move() manually eg: v.push_back(std::move(str)) when you > need move functionality can become tedious. Are there any or will there be any > inserters that use move() by default? > If the argument is an rvalue, move semantics should be used per default whenever a method that accepts a rvalue reference is available. If it's a lvalue, and you want move semantics, you have to specify it with std::move(). This makes sense, since move semantics, in principle, modifies the argument.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2022-07-19 07:24 +0000 |
| Message-ID | <tb5m7d$dsn$1@gioia.aioe.org> |
| In reply to | #85470 |
Manfred <noname@add.invalid> wrote:
> On 7/18/2022 5:55 PM, Muttley@dastardlyhq.com wrote:
>> Having to to call move() manually eg: v.push_back(std::move(str)) when you
>> need move functionality can become tedious. Are there any or will there be any
>> inserters that use move() by default?
>>
> If the argument is an rvalue, move semantics should be used per default
> whenever a method that accepts a rvalue reference is available. If it's
> a lvalue, and you want move semantics, you have to specify it with
> std::move().
> This makes sense, since move semantics, in principle, modifies the argument.
Since the object being given to v.push_back() has a name ("str" in this
case), it cannot be an rvalue. Pretty much by definition named objects
are not rvalues.
That's the reason why you have to cast it to "rvalue" in order to have
it treated like it were one.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web