move-semantics

Why parameters of universal reference needs to be casted, before used?

我的未来我决定 提交于 2019-12-21 16:51:52
问题 In the lecture about universal references, Scott Meyers (at approximately 40th minute) said that objects that are universal references should be converted into real type, before used. In other words, whenever there is a template function with universal reference type, std::forward should be used before operators and expressions are used, otherwise a copy of the object might be made. My understanding of this is in the following example : #include <iostream> struct A { A() { std::cout<<"constr"

Vector reallocation uses copy instead of move constructor

孤者浪人 提交于 2019-12-21 07:59:22
问题 Hi I created a class Foo with a noexcept move constructor using gcc 4.7 and set the vector reserve size to 2 so that it would have to reallocate the size when adding the 3rd item. It seems it is calling the copy constructor instead of the move constructor when doing this. Am I missing something here? #include <vector> #include <iostream> class Foo { public: Foo(int x) : data_(x) { std::cout << " constructing " << std::endl; } ~Foo() { std::cout << " destructing " << std::endl; } Foo& operator

Why do we need to set rvalue reference to null in move constructor?

僤鯓⒐⒋嵵緔 提交于 2019-12-21 07:55:58
问题 //code from https://skillsmatter.com/skillscasts/2188-move-semanticsperfect-forwarding-and-rvalue-references class Widget { public: Widget(Widget&& rhs) : pds(rhs.pds) // take source’s value { rhs.pds = nullptr; // why?? } private: struct DataStructure; DataStructure *pds; }; I can't understand the reason for setting rhd.pds to nullptr . What will happen if we remove this line : rhs.pds = nullptr; 回答1: Some details of the class have been removed. In particular, the constructor dynamically

To return std::move (x) or not?

好久不见. 提交于 2019-12-21 07:30:10
问题 Are std::vector<double> foo () { std::vector<double> t; ... return t; } and std::vector<double> foo () { std::vector<double> t; ... return std::move (t); } equivalent ? More precisely, is return x always equivalent to return std::move (x) ? 回答1: They're not equivalent, and you should always use return t; . The longer version is that if and only if a return statement is eligible for return value optimization, then the returnee binds to rvalue reference (or colloquially, "the move is implicit")

What is the rationale for self-assignment-unsafe move assignment operators in the standard library?

删除回忆录丶 提交于 2019-12-21 07:20:07
问题 The standard library policy about move assignment is that the implementation is allowed to assume that self-assignment will never happen; this seems to me a really bad idea, given that: the "regular" ("copy") assignment contract in C++ has always been regarded as safe against self-assignment; now we have yet another incoherent corner case of C++ to remember and to explain - and a subtly dangerous one, too; I think we all agree that what is needed in C++ is not more hidden traps; it

Why isn't move construction used when initiating a vector from initializer list (via implicit constructor)

安稳与你 提交于 2019-12-21 04:51:11
问题 To demo move semantics, I wrote the following example code, with an implicit constructor from int. struct C { int i_=0; C() {} C(int i) : i_( i ) {} C( const C& other) :i_(other.i_) { std::cout << "A copy construction was made." << i_<<std::endl; } C& operator=( const C& other) { i_= other.i_ ; std::cout << "A copy assign was made."<< i_<<std::endl; return *this; } C( C&& other ) noexcept :i_( std::move(other.i_)) { std::cout << "A move construction was made." << i_ << std::endl; } C&

push_back() and emplace_back() behind the scenes

ⅰ亾dé卋堺 提交于 2019-12-21 03:43:15
问题 I'm currently learning C++ on my own, and I am curious about how push_back() and emplace_back() work under the hood. I've always assumed that emplace_back() is faster when you are trying to construct and push a large object to the back of a container, like a vector. Let's suppose I have a Student object that I want to append to the back of a vector of Students. struct Student { string name; int student_ID; double GPA; string favorite_food; string favorite_prof; int hours_slept; int birthyear;

Generic conversion operator templates and move semantics: any universal solution?

独自空忆成欢 提交于 2019-12-21 03:35:14
问题 This is a follow-up of Explicit ref-qualified conversion operator templates in action. I have experimented with many different options and I am giving some results here in an attempt to see if there is any solution eventually. Say a class (e.g. any) needs to provide conversion to any possible type in a convenient, safe (no surprises) way that preserves move semantics. I can think of four different ways. struct A { // explicit conversion operators (nice, safe?) template<typename T> explicit

Move Assignment incompatible with Standard Copy and Swap

心不动则不痛 提交于 2019-12-21 03:12:18
问题 Testing out the new Move Semantics. I just asked about an issues I was having with the Move Constructor. But as it turns out in the comments the problem is really that the "Move Assignment" operator and "Standard Assignment" operator clash when you use the standard "Copy and Swap" idiom. This is the class I am using: #include <string.h> #include <utility> class String { int len; char* data; public: // Default constructor // In Terms of C-String constructor String() : String("") {} // Normal

Move semantics - what it's all about? [duplicate]

筅森魡賤 提交于 2019-12-20 12:38:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Can someone please explain move semantics to me? Could someone point me to a good source or explain it here what are the move semantics? 回答1: Forget about C++0x for the moment. Move semantics are something that is language independent -- C++0x merely provides a standard way to perform operations with move semantics. Definition Move semantics define the behaviour of certain operations. Most of the time they are