Q:

Why do I need a copy constructor for type T?

I am a little uncertain when the copy constructor is needed. For example, given this function:
template<class T>
T max(const T* array, int size) {
    T result = array[0];
    for (int i = 1; i < size; ++i) {
        if (result < array[i]) {
            result = array[i];
        }
    }
    return result;
}

What is the reason that I need a copy constructor for the type T? I think it must be because we return by value. Does this line T result = array[0]; also need the copy constructor?

A:

What is the reason that I need copy constructor for the type T?

T result = array[0];

This is known as a copy initialization and invokes the copy constructor for the type T. Type T will require a copy constructor for this line to succeed.

I think that it's must be because that we return by a value, and so we need copy constructor for T type.

return result;

For the most part, your assumption is correct for the return value. However, it isn't necessary for a copy constructor to be defined in this case. To implement move semantics, you can implement a move constructor which will remove the need for the copy constructor, since the local variable result will be "moved" instead of "copied" from. Move semantics remove the need for unnecessary copies of large objects when returning them from a function, since those large objects will not be accessible after the function returns.

