move

C++ how to move object to a nullptr

情到浓时终转凉″ 提交于 2020-07-22 21:39:24
问题 I am thinking a strange use case where I want to move an object to a nullptr. Maybe I should give an code fragment: class Objpair { public: Objpair(Obj&& a, Obj&&b) : first(&a), second(&b) { } private: Obj* first; Obj* second; }; The problem is that when a and b is out of scope, the first and second pointer will be dangling. If I can move Object a onto the first pointer then there should be no problem of double free and scoping issues. If the member first were declared as Obj not Obj* pointer

How do global consts that are not copy or clone work in Rust?

二次信任 提交于 2020-07-08 03:55:10
问题 Say I have the following snippet (playground) struct A { pub val: u32 } const GLOBAL_A: A = A {val: 2}; fn main() { let some_a: A = GLOBAL_A; let other_a: A = GLOBAL_A; println!("double val = {}", some_a.val + other_a.val); } Since A is neither Clone nor Copy , I would assume the value of GLOBAL_A would be moved. That does not make much sense for a const and as shown cannot be the case anyways since it can be "moved" twice. What are the rules that allow the above snippet to work considering A

Is it possible to std::move local stack variables?

我是研究僧i 提交于 2020-07-04 06:10:45
问题 Please consider the following code: struct MyStruct { int iInteger; string strString; }; void MyFunc(vector<MyStruct>& vecStructs) { MyStruct NewStruct = { 8, "Hello" }; vecStructs.push_back(std::move(NewStruct)); } int main() { vector<MyStruct> vecStructs; MyFunc(vecStructs); } Why does this work? At the moment when MyFunc is called, the return address should be placed on the stack of the current thread. Now create the NewStruct object gets created, which should be placed on the stack as

Is it true that “std::forward” and “std::move” do not generate code?

有些话、适合烂在心里 提交于 2020-06-29 06:43:21
问题 Is it true that "std::forward" and "std::move" do not generate code? I saw this saying in << An Effective C++11/14 Sampler >>. The related code is at the footnote. Could somebody explain the code in detail? I would be very grateful to have some help with this question. As per the documentation(https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00416_source.html), which says that: /** * @brief Forward an lvalue. * @return The parameter cast to the specified type. * * This function is

Move git repo with git clone --mirror and git push --mirror

你离开我真会死。 提交于 2020-06-26 08:49:09
问题 I have to move a couple of repos from Gitlab to Bitbucket and discovered that the easiest way to do it seems to be: Create new empty repository in Bitbucket git clone --mirror git@gitlab.com:path/to/repo.git cd repo.git git remote set-url origin git@bitbucket.org:path/to/repo.git git push --mirror This seems to copy the entire repo including tags, all branches etc. and I haven't discovered any disadvantages of this method yet. However, as I haven't seen this method anywhere on the internet, I

How to run for loop on elements of a vector and change the vector inside the for loop and outside the for loop in rust?

耗尽温柔 提交于 2020-06-26 06:02:38
问题 I am new to Rust . I need to create a vector before a for loop. Run for loop on it. Change the vector inside the for loop. Then Change the vector after the for loop. I tried the following code and tried to use immutable borrow but both did not work. fn main() { let mut vec1 = vec![4, 5]; vec1.push(6); for i in vec1 { if i % 2 == 0 { vec1.push(7); } } vec1.push(8); println!("vec1={:?}", vec1); } I expect to compile and change the vector inside and after the for loop. But it shows this error

How does std::move invalidates the value of original variable?

限于喜欢 提交于 2020-06-25 05:44:23
问题 In the following examples from cpp reference: #include <iostream> #include <utility> #include <vector> #include <string> int main() { std::string str = "Hello"; std::vector<std::string> v; // uses the push_back(const T&) overload, which means // we'll incur the cost of copying str v.push_back(str); std::cout << "After copy, str is \"" << str << "\"\n"; // uses the rvalue reference push_back(T&&) overload, // which means no strings will be copied; instead, the // Contents of str will be moved

Why is iterating over a collection via `for` loop considered a “move” in Rust?

∥☆過路亽.° 提交于 2020-06-21 05:35:51
问题 I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `v` --> src\main.rs:7:22 | 2 | let v = vec![100, 32, 57]; | - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait 3 | for i in v { | - | | | value moved here | help: consider borrowing to avoid moving into the for loop: `&v` ... 7 | println!("{:?}", v); | ^ value

Why is iterating over a collection via `for` loop considered a “move” in Rust?

家住魔仙堡 提交于 2020-06-21 05:34:58
问题 I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `v` --> src\main.rs:7:22 | 2 | let v = vec![100, 32, 57]; | - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait 3 | for i in v { | - | | | value moved here | help: consider borrowing to avoid moving into the for loop: `&v` ... 7 | println!("{:?}", v); | ^ value

delete NAs to move data up, no stairs anymore

牧云@^-^@ 提交于 2020-06-04 02:22:35
问题 The data file that I get from an experiment looks like this VP Selbst ES eigForm eigES andForm andES MM MMRT Wunschform Geschlecht Muttersprache Alter 99 1 unhöflich NA NA NA NA NA NA 99 5 gesellig NA NA NA NA NA NA 99 6 ehrlich NA NA NA NA NA NA 99 NA 5 gründlich NA NA NA NA NA 99 NA 6 treu NA NA NA NA NA 99 NA 5 romantisch NA NA NA NA NA 99 NA NA 6 stark NA NA NA NA 99 NA NA 3 klein NA NA NA NA 99 NA NA 5 rational NA NA NA NA 99 NA ordentlich NA NA ["y"] [0.4] NA NA NA NA 99 NA sentimental