How to allow movement of a structure and prevent the assignment and creation of a copy of a class

Is there a way to enable the move construct and prevent the creation and assignment of a copy. I can think of several classes with file pointers and buffer pointers (resource handles, etc.) that will benefit from copying and copy destination.

I am using VC2010 and GCC 4.5.2. I know that I would have to declare empty private assignments and copy constructors in the headers of the VC2010 class, and as far as I know, GCC will allow any signature to be deleted after the method does the same.

If anyone has a good example of such a skeletal class, and I would be very grateful for its advantages. thanks in advance John

Here is an example of a class for which I would like to allow moves, but I would also like to prevent direct sorting. Is it just a matter of creating a copy constructor and operator = private?

class LoadLumScanner_v8002 : public ILoadLumScanner { public: // default constructor LoadLumScanner_v8002(); // copy constructor LoadLumScanner_v8002(const LoadLumScanner_v8002& rhs); // move constructor LoadLumScanner_v8002(LoadLumScanner_v8002&& rhs); // non-throwing copy-and-swap idiom unified assignment inline LoadLumScanner_v8002& operator=(LoadLumScanner_v8002 rhs) { rhs.swap(*this); return *this; } // non-throwing-swap idiom inline void swap(LoadLumScanner_v8002& rhs) throw() { // enable ADL (not necessary in our case, but good practice) using std::swap; // swap base members // ... // swap members swap(mValidatedOk, rhs.mValidatedOk); swap(mFile, rhs.mFile); swap(mPartNo, rhs.mPartNo); swap(mMediaSequenceNo, rhs.mMediaSequenceNo); swap(mMaxMediaSequenceNo, rhs.mMaxMediaSequenceNo); swap(mLoadListOffset, rhs.mLoadListOffset); swap(mFirstLoadOffset, rhs.mFirstLoadOffset); swap(mLoadCount, rhs.mLoadCount); swap(mLoadIndex, rhs.mLoadIndex); swap(mLoadMediaSequenceNo, rhs.mLoadMediaSequenceNo); swap(mLoadPartNo, rhs.mLoadPartNo); swap(mLoadFilePath, rhs.mLoadFilePath); } // destructor virtual ~LoadLumScanner_v8002(); } 
+4
source share
1 answer

Both of the two solutions you mentioned work fine.

1.

 class MoveOnly { MoveOnly(const MoveOnly&); MoveOnly& operator=(const MoveOnly&); public: MoveOnly(MoveOnly&&); MoveOnly& operator=(MoveOnly&&); }; 

2.

 class MoveOnly { public: MoveOnly(const MoveOnly&) = delete; MoveOnly& operator=(const MoveOnly&) = delete; MoveOnly(MoveOnly&&); MoveOnly& operator=(MoveOnly&&); }; 

The signature "= delete" is new with C ++ 11 (as well as a reference to rvalue) and means, in fact, the same as the C ++ 03 method (declare private and not define). The advantage of the C ++ 11 solution is that it is likely to be erroneous when it fails during compilation, and not a delay before the link time.

Your compiler may not yet support "= delete", in which case you will have to abandon the first solution.

The third solution is to use copy elements by default:

 class MoveOnly { public: MoveOnly(MoveOnly&&); MoveOnly& operator=(MoveOnly&&); }; 

When a special motion element is declared, regardless of whether it was the default or not, then the compiler will implicitly add remote instances unless you declare them otherwise. Your compiler may or may not perform this function yet.

+9
source

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


All Articles