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


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

Re: Do any STL inserters use std::move() by default?

From Gawr Gura <gawrgura@mail.hololive.com>
Newsgroups comp.lang.c++
Subject Re: Do any STL inserters use std::move() by default?
Date 2022-07-18 10:25 -0700
Organization Hololive EN.
Message-ID <tb452f$fp7g$1@dont-email.me> (permalink)
References <tb3vor$1foi$1@gioia.aioe.org>

Show all headers | View raw


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.

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


Thread

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

csiph-web