move

std::move inside move assignment operator

主宰稳场 提交于 2019-12-23 09:44:43
问题 I read in another question that when implementing a move constructor it is good practice to std::move each member in the initializer list because if the member happens to be another object then that objects move constructor will be called. Like so... //Move constructor Car::Car(Car && obj) : prBufferLength(std::move(obj.prBufferLength)), prBuffer(std::move(obj.prBuffer)) { obj.prBuffer = nullptr; obj.prBufferLength = 0; } However in all the sample move assignment operators I've seen, there

Preventing move semantics during pattern matching

▼魔方 西西 提交于 2019-12-23 08:49:09
问题 I have a silly example here, just to demonstrate an issue I'm running into with another library and pattern matching. struct Person { name: String, age: i32, choice: Choices } #[derive(Debug)] enum Choices { Good, Neutral, Evil } fn find(p: Person) { match (p.choice, p.age) { (Choices::Good, a) if a < 80 => { announce(p); } (_, a) if a >= 80 => { println!("You're too old to care."); } _ => { println!("You're not very nice!") } } } fn announce(p: Person) { println!("Your name is {}. You are {:

Why is the move constructor not called? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-23 07:38:46
问题 This question already has answers here : What are copy elision and return value optimization? (4 answers) Copy constructor not called? (2 answers) Closed 3 years ago . As per my understanding, the move constructor will be called when there is a temporary object created. Here the getA() function is returning a temporary object but my program is not printing the message from the move constructor: #include <iostream> using namespace std; class A { public: A() { cout<<"Hi from default\n"; } A(A &

Using the move constructor to throw exceptions? (C++)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 07:22:44
问题 If I have an object e of type Error which implements a move constructor, will throwing std::move( e ) use the move constructor of Error to "duplicate" e , so does it avoid making an actual copy of the object? So if I have Error e; throw std::move( e ); will the copy constructor of Error be called or not? This is of interest when your move constructor is noexcept (as it should be), but your copy constructor isn't. 回答1: § 15.1 [except.throw]: Throwing an exception copy-initializes (8.5, 12.8) a

c++: confusion about forwarding reference

寵の児 提交于 2019-12-23 06:31:07
问题 I read this (incredibly well written) article about Forwarding Reference in C++11 by Scott Meyers. Now, focus on this part of the article: template <class... Args> void emplace_back(Args&&... args); // deduced parameter types ⇒ type deduction; ... // && ≡ universal references So, in contrast with other cases, the ellipses doesn't make the && an rvalue reference, but it's still universal references. From what I've understood, when we have universal references, we can call the function passing

move files with specific extension to folder in higher hierarchy

不羁的心 提交于 2019-12-23 04:07:48
问题 All my files are in specific folders: 17\1\1\PRO 17\1\2\PRO 17\2\1\PRO xx\xx\xx\PRO 17 is the year (so 18 for next year etc) the first 1 is the folder specifying the case number (can be up to 100). The second 1 is the sub parts on the case number. That last 1 has a folder PRO in it where all data resides. We need to move these files, but the files need to stay inside their respective "PRO" folders. For example: a file in 17\1\1\pro\xxx\www\ needs to go to 17\1\1\pro\movies a file in 17\2\2

Java Move File With Certain File Extension

南笙酒味 提交于 2019-12-23 03:15:18
问题 Hi i'm working on a simple program and for the set up of the program i need the program to check a directory for zip files and any zip files in there need to be moved into another folder. Lets say i have folder1 and it contains 6 zip files and then i have another folder called folder2 that i need all the zips and only the zips in folder1 moved to folder2 Thank you for any help one this problem. Btw i'm a noob so any code samples would be greatly appreciated 回答1: For each file in folder1 , use

Multiple implementation caused by rvalue parameter

对着背影说爱祢 提交于 2019-12-22 18:50:33
问题 Here is my test code: void test(std::vector<int> vec){}; void test(std::vector<int> && vec){}; int main(int argc, char * argv[]) { std::vector<int> v; test(v); test(std::move(v)); return 0; } When I try to call test(std::move(v)) , I was told test is multiply implemented. Obviously I have used std::move making v a rvalue. Won't test(std::vector<int> &&) be called specifically? 回答1: This isn't directly related to rvalues, or moving. The same happens with an lvalue reference overload void test

Multiple implementation caused by rvalue parameter

空扰寡人 提交于 2019-12-22 18:49:58
问题 Here is my test code: void test(std::vector<int> vec){}; void test(std::vector<int> && vec){}; int main(int argc, char * argv[]) { std::vector<int> v; test(v); test(std::move(v)); return 0; } When I try to call test(std::move(v)) , I was told test is multiply implemented. Obviously I have used std::move making v a rvalue. Won't test(std::vector<int> &&) be called specifically? 回答1: This isn't directly related to rvalues, or moving. The same happens with an lvalue reference overload void test

is_assignable and std::unique_ptr

橙三吉。 提交于 2019-12-22 05:36:18
问题 Here is a test file from gcc, live demo struct do_nothing { template <class T> void operator()(T*) {} }; int main() { int i = 0; std::unique_ptr<int, do_nothing> p1(&i); std::unique_ptr<int> p2; static_assert(!std::is_assignable<decltype(p2), decltype(p1)>::value, ""); // note ! here. } std::is_assignable If the expression std::declval<T>() = std::declval<U>() is well-formed in unevaluated context, provides the member constant value equal true. Otherwise, value is false. Access checks are