Concept Subspace :: sus :: mem :: Clone

template <class T>
concept Clone
requires
(Copy<T> && !__private::HasCloneMethod<
                    std::remove_const_t<std::remove_reference_t<T>>>) ||
    (!Copy<T> && Move<T> &&
     __private::HasCloneMethod<
         std::remove_const_t<std::remove_reference_t<T>>>)

A Clone type can make a new copy of itself.

When a type is small enough to be passed in registers (typically at most the size of two pointers) and copying is the same as moving, prefer to make the type Copy, which also implies Clone.

A type T is Clone if one of the following holds:

  • Is Copy. Meaning it has a copy constructor and assignment.
  • Has a clone() const -> T method, and is Move.

This concept tests the object type of T, looking through reference types T& or const T&. To test the reference types themselves (which can always be copied as a pointer) in generic code that can handle reference types and won't copy the objects behind them, use CloneOrRef.

A Clone type may also provide a clone_from(const T& source) method to have sus::mem::clone_into() perform copy-assignment from source, in order to reuse its resources and avoid allocations. In this case the type is also CloneFrom.

It is not valid to be Copy and also have a clone() method, as it becomes ambiguous. One outcome of this is a collection type should only implement a clone() method if the type T within is (Clone<T> && !Copy<T>), and should only implement copy constructors if the type T within is Copy<T>.