move-semantics

std::vector::emplace_back and std::move

早过忘川 提交于 2020-05-09 19:05:31
问题 Is there any advantage of using std::vector::emplace_back and std::move together? or it is just redundant since std::vector::emplace_back will do an inplace-construction? Cases for clarification: std::vector<std::string> bar; First: bar.emplace_back(std::move(std::string("some_string"))); Second: std::string str("some_string"); bar.emplace_back(std::move(str)); Third: bar.emplace_back(std::move("some_string")); 回答1: In the second version, there is an advantage. Calling emplace_back will call

std::vector::emplace_back and std::move

被刻印的时光 ゝ 提交于 2020-05-09 19:05:15
问题 Is there any advantage of using std::vector::emplace_back and std::move together? or it is just redundant since std::vector::emplace_back will do an inplace-construction? Cases for clarification: std::vector<std::string> bar; First: bar.emplace_back(std::move(std::string("some_string"))); Second: std::string str("some_string"); bar.emplace_back(std::move(str)); Third: bar.emplace_back(std::move("some_string")); 回答1: In the second version, there is an advantage. Calling emplace_back will call

Is there a way to have a Rust closure that moves only some variables into it?

给你一囗甜甜゛ 提交于 2020-05-06 17:46:53
问题 I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this value, they do slightly different work. Each of these threads should be able to read the general settings struct. use crossbeam; // 0.7.3 struct Settings { // ... many fields } const MAX_FEASIBLE_SCORE: u8 = 10; fn example(settings: Settings) {

Is there a way to have a Rust closure that moves only some variables into it?

依然范特西╮ 提交于 2020-05-06 17:45:17
问题 I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this value, they do slightly different work. Each of these threads should be able to read the general settings struct. use crossbeam; // 0.7.3 struct Settings { // ... many fields } const MAX_FEASIBLE_SCORE: u8 = 10; fn example(settings: Settings) {

std::move a const std::vector in a lambda capture

独自空忆成欢 提交于 2020-04-30 07:44:07
问题 Motivation: I'm trying to transfer a std::vector<std::unique_ptr<some_type>> to a different thread, via a lambda capture. Since I need the vector to not be cleaned up when the function goes out of scope, I need to take it by value (and not by reference). Since it's a vector of unique_ptrs, I need to move (and not copy) it into the capture. I'm using a generalized lambda capture to move the vector while capturing. Minimal program to illustrate the concept: auto create_vector(){ std::vector<std

Why is std::map's move constructor not noexcept?

青春壹個敷衍的年華 提交于 2020-03-17 10:21:30
问题 As said by cppreference.com, Maps are usually implemented as red-black trees. So moving a std::map is just moving the pointer to the root node + other information such as size. Why is std::map 's move constructor not marked as noexcept ? 回答1: It is because I couldn't talk all of the implementors into a resource-less state that the map could be put into. For example an implementation needs to have an end-node to point to, even in the default-constructed state. Implementations are allowed, but

Why is std::map's move constructor not noexcept?

你。 提交于 2020-03-17 10:20:37
问题 As said by cppreference.com, Maps are usually implemented as red-black trees. So moving a std::map is just moving the pointer to the root node + other information such as size. Why is std::map 's move constructor not marked as noexcept ? 回答1: It is because I couldn't talk all of the implementors into a resource-less state that the map could be put into. For example an implementation needs to have an end-node to point to, even in the default-constructed state. Implementations are allowed, but

What are the rules for noexcept on default defined move constructors?

我只是一个虾纸丫 提交于 2020-02-21 08:25:52
问题 Especially in connection with std::vector it is important that types are noexcept movable when possible. So when declaring a move constructor = default like in struct Object1 { Object1(Object1 &&other) = default; }; std::is_nothrow_move_constructible<Object1>::value will be true as every member (0 here) of Object1 is nothrow-move-constructible, which is answered here. Yet what happens if the move copy constructor is only declared and then later = default defined like in the following code?

Overload on reference, versus sole pass-by-value + std::move?

佐手、 提交于 2020-01-30 13:48:12
问题 It seems the main advice concerning C++0x's rvalues is to add move constructors and move operators to your classes, until compilers default-implement them. But waiting is a losing strategy if you use VC10, because automatic generation probably won't be here until VC10 SP1, or in worst case, VC11. Likely, the wait for this will be measured in years. Here lies my problem. Writing all this duplicate code is not fun. And it's unpleasant to look at. But this is a burden well received, for those

std::move or std::forward when assigning universal constructor to member variable in C++

ぃ、小莉子 提交于 2020-01-29 10:44:25
问题 Consider the following classes foo1 and foo2 template <typename T> struct foo1 { T t_; foo1(T&& t) : t_{ std::move(t) } { } }; template <typename T> struct foo2 { foo1<T> t_; foo2(T&& t) : t_{ std::forward<T>(t) } { } }; Is it always the case that the constructor of foo1 represents the correct way to initialise the member variable T ? i.e. by using std::move . Is it always the case that the constructor of foo2 represents the correct way to initialise the member variable foo1<T> due to needing