Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy but not move

Tags:

c++11

In C++0x, is it legal / expected that some classes can be copied but not moved? I'm looking at implementing a heterogenous class that resizes, and I'm not sure I could handle it if some classes needed copying and some needed moving.

like image 679
Puppy Avatar asked Jan 01 '11 23:01

Puppy


People also ask

How do you copy instead of move?

Press and hold the Ctrl key while you drag and drop to always copy. Press and hold the Shift key while you drag and drop to always move.

How do I move files instead of copying on a Mac?

Let's start with the most basic move when it comes to file handling. Drag and drop a file on the same drive on your Mac. When you do that, your computer will automatically move that file rather than make a copy of it. Click on the file you'd like to move to select it.

Is it quicker to copy or move?

If we are cutting(moving) within a same disk, then it will be faster than copying because only the file path is modified, actual data is on the disk. If the data is copied from one disk to another, it will be relatively faster than cutting because it is doing only COPY operation.

Why can't I move files in File Explorer?

Fortunately, you can easily fix it without having to restart your computer or configure your system settings. In File Explorer, click any file or folder and hold the left button on your mouse. Then, press the Esc key. Now, try dragging and dropping again.


1 Answers

Yes, it's legal for a class to be copyable but not movable:

class MyClass {
public:
    /* Copyable... */
    MyClass(const MyClass&);
    MyClass& operator= (const MyClass&);

    /* ... but not movable. */
    MyClass(MyClass&&) = delete;
    MyClass& operator= (MyClass&&) = delete;
};

However, I can't think of a good reason as to why anyone would want to do this. Knowing C++ coders (like me!) though, I think that you should anticipate that this might come up.

Out of curiosity, what code are you relying on that would break if a class was copyable but not movable?

like image 137
templatetypedef Avatar answered Jan 04 '23 17:01

templatetypedef