move-semantics

How to implement prepend for a linked list without needing to assign to a new variable?

别说谁变了你拦得住时间么 提交于 2019-12-02 06:25:01
问题 Something told me how to implement a linked list: enum List { Cons(u32, Box<List>), Nil, } impl List { fn prepend(self, elem: u32) -> List { Cons(elem, Box::new(self)) } } When I want to use prepend , I need to do the following: list = list.prepend(1); However, I want to create a function that does not need to create a new variable every time prepend returns. I just want to change the list variable itself using prepend : list.prepend(1); Here is one implementation that I come up with, but it

Move constructor is not called when throwing an exception

故事扮演 提交于 2019-12-02 05:08:07
问题 I have a variable which accumulates the current exception and needs to get cleaned when the current exception gets thrown (so that the same error does not get reported again). The problem is that throw std::move(ex); does not call the move constructor (which would clean ex ), but rather calls a copy constructor (so that ex stays with the already thrown errors too). A MVCE follows: #include <iostream> #include <stdexcept> #include <string> using namespace std; class ThrowMoveTest : exception {

Does it make sense to reuse destructor logic by using std::swap in a move assignment operator?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 02:17:05
Consider the following: class Example : boost::noncopyable { HANDLE hExample; public: Example() { hExample = InitializeHandle(); } ~Example() { if (hExample == INVALID_HANDLE_VALUE) { return; } FreeHandle(hExample); } Example(Example && other) : hExample(other.hExample) { other.hExample = INVALID_HANDLE_VALUE; } Example& operator=(Example &&other) { std::swap(hExample, other.hExample); //? return *this; } }; My thinking here is that the destructor will be running on "other" shortly, and as such I don't have to implement my destructor logic again in the move assignment operator by using swap.

How to implement prepend for a linked list without needing to assign to a new variable?

匆匆过客 提交于 2019-12-02 01:30:44
Something told me how to implement a linked list: enum List { Cons(u32, Box<List>), Nil, } impl List { fn prepend(self, elem: u32) -> List { Cons(elem, Box::new(self)) } } When I want to use prepend , I need to do the following: list = list.prepend(1); However, I want to create a function that does not need to create a new variable every time prepend returns. I just want to change the list variable itself using prepend : list.prepend(1); Here is one implementation that I come up with, but it's not right: fn my_prepend(&mut self, elem: u32) { *self = Cons(elem, Box::new(*self)); } The error is:

Is the contents of a pointer to a unique_ptr's contents valid after the unique_ptr is moved?

你离开我真会死。 提交于 2019-12-01 23:58:09
问题 I've been led to understand that calling a member function on the contents of a moved-from std::unique_ptr is undefined behaviour. My question is: if I call .get() on a unique_ptr and then move it, will the original .get() pointer continue to point to the contents of the original unique pointer? In other words, std::unique_ptr<A> a = ... A* a_ptr = a.get(); std::unique_ptr<A> a2 = std::move(a); // Does *a_ptr == *a2? I think it does, but I want to make sure. ('contents' is probably the wrong

Is the contents of a pointer to a unique_ptr's contents valid after the unique_ptr is moved?

人走茶凉 提交于 2019-12-01 21:34:15
I've been led to understand that calling a member function on the contents of a moved-from std::unique_ptr is undefined behaviour. My question is: if I call .get() on a unique_ptr and then move it, will the original .get() pointer continue to point to the contents of the original unique pointer? In other words, std::unique_ptr<A> a = ... A* a_ptr = a.get(); std::unique_ptr<A> a2 = std::move(a); // Does *a_ptr == *a2? I think it does, but I want to make sure. ('contents' is probably the wrong word. I mean the data you get when you dereference the pointer) Merely moving the unique_ptr only

move semantics std::move how use it

回眸只為那壹抹淺笑 提交于 2019-12-01 21:03:19
#include <type_traits> template<class T> typename std::remove_reference<T>::type&& move(T&& v) { return v; } void main() { int a; move(a); } Why doesn't this code compile? error C2440: 'return' : impossible to convert 'int' in 'int &&' This is straight out of the C++0x draft standard (§20.2.3/6): template <class T> typename remove_reference<T>::type&& move(T&& t) noexcept; Returns : static_cast<typename remove_reference<T>::type&&>(t) . Consequently, if you change your move implementation to the following, it works just fine: template<class T> typename std::remove_reference<T>::type&& move(T&&

Why does get helper of std::tuple return rvalue reference instead of value

a 夏天 提交于 2019-12-01 18:11:36
问题 If you look at get , the helper function for std::tuple , you will notice the following overload: template< std::size_t I, class... Types > constexpr std::tuple_element_t<I, tuple<Types...> >&& get( tuple<Types...>&& t ); In other words, it returns an rvalue reference when the input tuple is an rvalue reference itself. Why not return by value, calling move in the function body? My argument is as follows: the return of get will either be bound to a reference, or to a value (it could be bound

Construct-in-place an unmoveable object in a map

送分小仙女□ 提交于 2019-12-01 17:52:47
I'm trying to construct an object in a map that contains an atomic, so it can neither be copied nor moved AFAICT. My reading of C++ reference is that map emplace should be able to do this. But the following code does not compile because of deleted or non-existent constructors. Using make_pair does not help. #include <atomic> #include <unordered_map> class Z { std::atomic<int> i; }; std::unordered_map<int, Z> map; void test(void) { map.emplace(0, Z()); // error map[0] = Z(); // error } Is this possible, and if not, why not? EDIT: Compiler is gcc 4.8.1, on Linux map.emplace(std::piecewise

Why does get helper of std::tuple return rvalue reference instead of value

混江龙づ霸主 提交于 2019-12-01 17:44:24
If you look at get , the helper function for std::tuple , you will notice the following overload: template< std::size_t I, class... Types > constexpr std::tuple_element_t<I, tuple<Types...> >&& get( tuple<Types...>&& t ); In other words, it returns an rvalue reference when the input tuple is an rvalue reference itself. Why not return by value, calling move in the function body? My argument is as follows: the return of get will either be bound to a reference, or to a value (it could be bound to nothing I suppose, but this shouldn't be a common use case). If it's bound to a value, then a move