Simple Single Linked List I'm currently studying data structures, I have implemented simple single linked list as shown below. 

I would like to know how can I improve it?

#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;cassert&gt;

template &lt;typename T&gt;
class List 
{
    struct node 
    {
        T value;
        std::unique_ptr&lt;node&gt; next;
    };

public:
    List();

    void add(T value);

    T first() const;
    T value(std::size_t position) const;

    std::size_t length() const;
    void clear();

private:
    std::unique_ptr&lt;node&gt;   mFirst;
    std::size_t             mLength;
};

template &lt;class T&gt;
List&lt;T&gt;::List()
    : mFirst(nullptr)
    , mLength()
{
}

template &lt;typename T&gt;
std::size_t List&lt;T&gt;::length() const
{
    return mLength;
}

template &lt;typename T&gt;
T List&lt;T&gt;::first() const
{
    assert(mFirst != nullptr);
    return mFirst-&gt;value;
}

template &lt;typename T&gt;
void List&lt;T&gt;::add(T value)
{
    auto next = std::move(mFirst);
    mFirst = std::make_unique&lt;node&gt;();
    mFirst-&gt;value = value;
    mFirst-&gt;next = std::move(next);
    mLength++;
}

template &lt;class T&gt;
T List&lt;T&gt;::value(std::size_t position) const
{
    assert(mFirst != nullptr);

    auto current = mFirst.get();

    for (auto i = 0u; i &lt; position; ++i)
    {
        current = current-&gt;next.get();
    }
    return current-&gt;value;
}

template &lt;class T&gt;
void List&lt;T&gt;::clear()
{
    assert(mFirst != nullptr);

    auto length = mLength;

    for (auto i = 0u; i &lt; length; ++i)
    {
        mFirst = std::move(mFirst-&gt;next);
        mLength--;
    }
}

int main()
{
    List&lt;int&gt; list;

    for (int i = 0; i &lt; 10; ++i)
        list.add(i);

    for (auto i = 0u; i &lt; list.length(); ++i)
        std::cout &lt;&lt; list.value(i) &lt;&lt; ' ';

    list.clear();

    std::cout &lt;&lt; "\n\n" &lt;&lt; list.length() &lt;&lt; "\n\n";
}
Well, there's already a good review by Barry, but I'll take a different tack:


In general, it's a good idea to use existing types (like smart-pointers) as building-blocks.
Still, you should not deform your code, causing loss of efficiency or clarity, just so you can use one, especially not in library-code (and such a basic container is that).

So, no smart-pointers for you, sometimes one really should get down to it.
As an aside, your clear has a bug: It tries to assign the deleter after already having deleted the source. See std::unique_ptr::operator=(std::unique_ptr&amp;&amp;).
There's a reason a node is generally a wholly-owned passive and dumb aggregate without any behavior: It would just get in the way of the List defining it properly.
You should use the same names for members all the containers in the standard-library use, so you can use standard algorithms.
For that same reason, and so you can efficiently iterate over all elements, you should provide iterators.
Sacrificing some space in the class itself for a size can be a reaonable idea. Similar arguments can be made for including a pointer (node**) to the end, so you can speed up push_back/emplace_back.
Consider putting the next-pointer first, that might allow the compiler to merge code for different template-arguments more often.




This is how you clear a list:

void List::clear() {
    for(node *head = this-&gt;head, *temp; head; head = temp) {
        temp = head-&gt;next;
        delete head;
    }
    this-&gt;head = nullptr;
}