After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable, I starting wondering if there are use cases for a class which can be copied but not moved. Technically, this is possible:
struct S
{
S() { }
S(S const& s) { }
S(S&&) = delete;
};
S foo()
{
S s1;
S s2(s1); // OK (copyable)
return s1; // ERROR! (non-movable)
}
Although S
has a copy constructor, it obviously does not model the CopyConstructible
concept, because that is in turn a refinement of the MoveConstructible
concept, which requires the presence of a (non-deleted) move constructor (see § 17.6.3.1/2, Table 21).
Is there any use case for a type like S
above, which is copyable but not CopyConstructible
and non-movable? If not, why is it not forbidden to declare a copy constructor and a deleted move constructor in the same class?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…