I was reading regarding move constructor and I did this code in VS 2013...
class Student { unique_ptr<string> pName_; public: Student(string name) : pName_(new string(name)) { } ~Student() { } Student(Student&&) = default; // Here I get the error. void printStudentName(void) { cout << *pName_ << endl; } }; int main(void) { vector<Student> persons; Student p = Student("Nishith"); persons.push_back(std::move(p)); persons.front().printStudentName(); return 0; }
I get the "Student::Student(Student&& )
: is not a special member function which can be defaulted" when I tried to compile it...
Can anyone explain me why I am getting this error?
A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying. For more information about move semantics, see Rvalue Reference Declarator: &&.
Constructor can not be declared virtual.
In C++, the compiler creates a default constructor if we don't define our own constructor. In C++, compiler created default constructor has an empty body, i.e., it doesn't assign default values to data members. However, in Java default constructors assign default values.
If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.
Because the VS2013 compiler doesn't support defaulted move constructors.
See the following note from MSDN:
Visual Studio does not support defaulted move constructors or move-assignment operators as the C++11 standard mandates. For more information, see the Defaulted and Deleted functions section of Support For C++11 Features (Modern C++).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With