Reputation: 2104
I have the following class:
class Student
{
private:
std::string firstName;
std::string lastName;
public:
Student():firstName(""), lastName("")
{
}
Student(const std::string &first, const std::string &last)
:firstName(first), lastName(last)
{
}
Student(const Student &student)
:firstName(student.firstName), lastName(student.lastName)
{
}
Student(Student &&student)
{
firstName=std::move(student.firstName);
lastName=std::move(student.lastName);
}
// ... getters and setters
};
I use it like this:
std::vector<std::shared_ptr<Student>> students;
std::shared_ptr<Student> stud1 = std::make_shared<Student>("fn1","ln1");
students.push_back(stud1);
Student stud2("fn2","ln2");
students.push_back(std::make_shared<Student>(std::move(stud2)));
From what I have read, the move constructor in automatically generated by the compiler.
Right now, when I step into this line students.push_back(std::make_shared<Student>(std::move(stud2)));
I reach the move constructor, which is ok.
If I comment out the move constructor when I step into that line I reach the copy constructor. I don't understand why this is happening.
Upvotes: 4
Views: 230
Reputation: 355019
Visual C++ 2012 does not implicitly generate move constructors or move assignment operators.
(The rules governing when move operations are and are not implicitly declared and defined changed several times during standardization; Visual C++ 2012 does not support the standardized (2011) set of rules.)
Upvotes: 3
Reputation: 45414
In your case, you may simple declare all these constructors =default
, e.g.
class student
{
std::string firstname, surname;
public:
student(student const&) = default;
student(student&&) = default;
student&operator=(student const&) = default;
student&operator=(student&&) = default;
// etc
};
and don't worry about the details: the compiler should sort this out and generate the appropriate call to std::string::string(string&&)
(move constructor).
EDIT Of course, this will not work with deficient compilers, but if you're tagging "C++11", then you should expect a C++11 answer.
Upvotes: 1