Skip to main content
added 349 characters in body
Source Link
Seth Carnegie
  • 75.3k
  • 22
  • 185
  • 252

I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here.

I am wondering why the following code tries to call B(const B&) instead of B(B&&):

#include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst; char* buf = new char[sizeof(B)]; B* bptr = new (buf) B(std::move(binst)); cout << bptr->data << endl; delete[] buf; } 

I didn't explicitly define any constructors, so B(std::move(binst)) should call the compiler generated B(B&&), no?

When I change B to

class B : public A { public: B() { } B(B&&) { } }; 

It compiles fine. Why is this?

It will be extremely inconvenient if this can't be fixed from the base class because I have a template class which uses placement new and move constructors like the example, and it will require every class that is not copyable (which is not and definitely should not be a requirement for use with my template class) to have an explicitly defined move constructor.

I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here.

I am wondering why the following code tries to call B(const B&) instead of B(B&&):

#include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst; char* buf = new char[sizeof(B)]; B* bptr = new (buf) B(std::move(binst)); cout << bptr->data << endl; delete[] buf; } 

I didn't explicitly define any constructors, so B(std::move(binst)) should call the compiler generated B(B&&), no?

When I change B to

class B : public A { public: B() { } B(B&&) { } }; 

It compiles fine. Why is this?

I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here.

I am wondering why the following code tries to call B(const B&) instead of B(B&&):

#include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst; char* buf = new char[sizeof(B)]; B* bptr = new (buf) B(std::move(binst)); cout << bptr->data << endl; delete[] buf; } 

I didn't explicitly define any constructors, so B(std::move(binst)) should call the compiler generated B(B&&), no?

When I change B to

class B : public A { public: B() { } B(B&&) { } }; 

It compiles fine. Why is this?

It will be extremely inconvenient if this can't be fixed from the base class because I have a template class which uses placement new and move constructors like the example, and it will require every class that is not copyable (which is not and definitely should not be a requirement for use with my template class) to have an explicitly defined move constructor.

Source Link
Seth Carnegie
  • 75.3k
  • 22
  • 185
  • 252

Why is this code trying to call the copy constructor?

I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here.

I am wondering why the following code tries to call B(const B&) instead of B(B&&):

#include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst; char* buf = new char[sizeof(B)]; B* bptr = new (buf) B(std::move(binst)); cout << bptr->data << endl; delete[] buf; } 

I didn't explicitly define any constructors, so B(std::move(binst)) should call the compiler generated B(B&&), no?

When I change B to

class B : public A { public: B() { } B(B&&) { } }; 

It compiles fine. Why is this?