Implicitly declared Move-Operations not reverting to copy?

I am reading N3291 "12.8. (11/15/28) Copying and moving class.copy class objects]" to fix that implicitly declared move constructor

  • performs an element moves all non-static data members (possibly through an appropriately defined T(T&&)
  • and if any non-static data member cannot be moved, will the implicit constructor move be marked as deleted and not copied as "backup"? (yes, relocation is defined for built-in types, but it's actually a copy).

and move-assign, using the appropriate T operator=(T&&) elements.

Example:

 struct CopyOnly { CopyOnly(); CopyOnly(const CopyOnly&); }; // declaring a copy means no implicit move. struct Question { std::vector<int> data_; CopyOnly copyOnly_; }; 

Question class

  • will have an implicitly declared constructor instance and assign
  • will have an implicitly declared move-constructor and move-assign, but they will =delete d , because the non-static data member data_ is copy-only, but not movable ?

Update . Question: for Question q; will std::move(q) work yet? Will a spare instance happen there? Or does an implicitly declared move-ctor cause the compiler to stop with an error? Here it compiles.

Update 2. What does the compiler generate for immovable data elements if I declare move-ctor Question(Question&&) =default ? Does it refuse to copy data?

+6
source share
1 answer

You are reading it wrong. This will break many C ++ 03 classes in the following cases:

 Question getQuestion(); Question q(getQuestion()); // use of deleted move constructor! 

Instead, FDIS says that the move constructor will be declared iff {there is no user-declared {copy constructor, {copy, move}, destructor} operator, and the implicitly declared move constructor will not be defined as remote}.

Relatively Update 2 . I was informed that if you explicitly specify a move constructor, it will be defined as deleted by the condition

for a move constructor, a non-static data element, or a direct or virtual base class with a type that does not have a move constructor and cannot be trivially copied.

Next, the move constructor will be defined as remote because CopyOnly not trivially copied.

  struct Question { std::vector<int> data_; CopyOnly copyOnly_; Question(Question&&) = default; }; 
+5
source

Source: https://habr.com/ru/post/897540/