Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Constructor - invalid type for defaulted constructor VS 2013

Tags:

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?

like image 842
NJMR Avatar asked Jul 04 '14 12:07

NJMR


People also ask

What does the default move constructor do?

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: &&.

Which of these is a invalid type of constructor in C++?

Constructor can not be declared virtual.

Does compiler provide default move constructor?

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.

Are move constructor automatically generated?

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.


1 Answers

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++).

like image 65
Roger Rowland Avatar answered Oct 19 '22 15:10

Roger Rowland