move-semantics

How does Rust move stack variables that are not Copyable?

微笑、不失礼 提交于 2020-01-01 08:25:11
问题 There is a great example of Rust's move semantics documented here: Rust Move Semantics on the Rust By Example website. I have a basic understanding of both cases demonstrated. The first being how a primitive can have a new alias and the original can still be used because the end result is a copy seeing as i32 utilizes the Copy trait. This makes good sense to me. Additionally, for many good reasons the second example makes sense in terms of having multiple aliases that refer to an i32 on the

move semantics std::move

帅比萌擦擦* 提交于 2020-01-01 08:24:08
问题 I don't understand very well the std::move function template <class T> typename remove_reference<T>::type&& move(T&& a) { return a; } why remove_reference ? could someone give me a simple explanation ? 回答1: Think about what happens if T is an lvalue reference, for example MyClass & . In that case, T && would become MyClass & && , and due to reference collapsing rules , this would be transformed into MyClass & again. To achieve the right result, typename remove_reference<MyClass&>::type&&

move semantics std::move

纵饮孤独 提交于 2020-01-01 08:24:07
问题 I don't understand very well the std::move function template <class T> typename remove_reference<T>::type&& move(T&& a) { return a; } why remove_reference ? could someone give me a simple explanation ? 回答1: Think about what happens if T is an lvalue reference, for example MyClass & . In that case, T && would become MyClass & && , and due to reference collapsing rules , this would be transformed into MyClass & again. To achieve the right result, typename remove_reference<MyClass&>::type&&

Move Semantics and Pass-by-Rvalue-Reference in Overloaded Arithmetic

删除回忆录丶 提交于 2020-01-01 04:23:08
问题 I am coding a small numeric analysis library in C++. I have been trying to implement using the latest C++11 features including move semantics. I understand the discussion and top answer at the following post: C++11 rvalues and move semantics confusion (return statement) , but there is one scenario that I still am trying to wrap my head around. I have a class, call it T , which is fully equipped with overloaded operators. I also have both copy and move constructors. T (const T &) { /

Is std::move really needed on initialization list of constructor for heavy members passed by value?

我与影子孤独终老i 提交于 2019-12-31 08:36:09
问题 Recently I read an example from cppreference.../vector/emplace_back: struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } My question: is this std::move really needed? My point is that this p_name is not used in the body of constructor, so, maybe, there is some rule in the language to use move

Workarounds for no 'rvalue references to *this' feature

谁说我不能喝 提交于 2019-12-31 08:12:08
问题 I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this behaviour as per proposal n2439 "Extending move semantics to *this", but it is not yet available in a release of gcc and won't be for a while. The code below is what I am ultimately aiming for, but is not currently possible. Until this feature is

Workarounds for no 'rvalue references to *this' feature

蹲街弑〆低调 提交于 2019-12-31 08:11:30
问题 I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this behaviour as per proposal n2439 "Extending move semantics to *this", but it is not yet available in a release of gcc and won't be for a while. The code below is what I am ultimately aiming for, but is not currently possible. Until this feature is

Warn if accessing moved object in C++11 [duplicate]

南笙酒味 提交于 2019-12-30 07:56:55
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What can I do with a moved-from object? After you called std::move and passed the result to a function, you generally have to assume that accessing the moved object later will result in undefined behavior. Are there tools that can detect those accesses and warn you. For example: { Widget w; foo(std::move(w)); // w may be undefined at this point w.doSomething(); // WARN } At least, gcc 4.7.2 and clang 3.2 with

How does the compiler know to move local variables?

家住魔仙堡 提交于 2019-12-30 04:36:10
问题 I'm curious as to exactly how this feature works. Consider something like std::unique_ptr<int> f() { std::unique_ptr<int> lval(nullptr); return lval; } This code compiles fine even for a move-only type, as the compiler implicitly moves it. But logically, for any return expression, determining whether or not the result refers to a local variable would be solving the Halting Problem- and if the compiler simply treated all local variables as rvalues in the return expression, then this would be

Function dual to std::move?

♀尐吖头ヾ 提交于 2019-12-29 09:05:43
问题 Let's assume I have a class with only one constructor: class T { public: T(BigClass&& big) : big(std::move(big)) {} ... SomeBigClass }; In most places the constructor is called on temporaries but in one place I need to make an explicit copy of BigClass because it is not a temporary and will be used multiple times in a loop: void foo(const BigClass& big) { while (...) { T t(std::make_a_copy(big)); ... } } Is there any function "dual" to std::move in C++11 or C++14 that would replace make_a