Puppy
Puppy

Reputation: 146940

Copy but not move

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.

Upvotes: 5

Views: 177

Answers (2)

templatetypedef
templatetypedef

Reputation: 372814

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?

Upvotes: 5

rubenvb
rubenvb

Reputation: 76579

I'm quite sure that making the move constructor protected/private is a way of limiting the ability to move objects of that class. If you can expect that in a template? I don't think so. You'd need std::enable_if for that kind of check.

Upvotes: 0

Related Questions