mercredi 26 octobre 2016

Default return value for a template variable

I have a template link list implementation in which

T List<T>::GetElementFromHead()

returns value from the head of the list. What value should be returned when the list is empty.

template <class T>
T List<T>::GetElementFromHead()
{
    T element;
    if (!IsEmpty())
    {
        element = Head->value;
    }
    return element;
}

If IsEmpty() returns true then return element; throws exception.

How do I return null or and empty value in this case?

Aucun commentaire:

Enregistrer un commentaire