Are move constructors produced automatically?

对着背影说爱祢 提交于 2019-11-26 09:36:48

问题


I have a big class holding a lot of STL containers.
Will the compiler automatically make a move constructor that will move those containers to the target or I have to make my own?


回答1:


A move constructor for a class X is implicitly declared as defaulted exactly when

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared copy assignment operator,
  • X does not have a user-declared move assignment operator,
  • X does not have a user-declared destructor, and
  • the move constructor would not be implicitly defined as deleted.

So for example, if your class has a class type data member that does not have a move constructor, your class will not get a move constructor even if it doesn't have any copy/move constructor declared, because the implicitly declared move constructor would be defined as deleted (because of that data member).




回答2:


Default move constructors are generally tied to default copy constructors. You get one when you get the other. However, if you write a copy constructor/assignment operator, then no default copy and move constructors/assignment operators are written. If you write one of either set, you must write them all.



来源:https://stackoverflow.com/questions/8283589/are-move-constructors-produced-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!