Functional-style list in C++ I've implemented a functional-style list structure in C++. It seems okay so far, and doing custom allocation for list nodes improves performance greatly. I omitted that part here for simplicity.

The mutability of the list is intentional, as the application I'm thinking of requires a lot of state updates. I wrote this code instead of using std::forward_list because


I need to instantiate the list with incomplete types.
The algorithm is recursive (some kind of tree search), so the code will look better with a functional data structure.


Any advice is welcome.

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

template&lt;typename T&gt;
struct List
{
    T head;
    std::unique_ptr&lt;List&lt;T&gt;&gt; tail;
};

template&lt;typename T&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; add(const T &amp;t, std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;&amp;l)
{
    return std::unique_ptr&lt;List&lt;T&gt;&gt;(new List&lt;T&gt;{t, std::move(l)});
}

template&lt;typename T&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; list()
{
    return std::unique_ptr&lt;List&lt;T&gt;&gt;();
}

template&lt;typename T, typename ...Ts&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; list(const T &amp;t, const Ts &amp;...ts)
{
    return add(t, list&lt;T&gt;(ts...));
}

template&lt;typename T&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; copy(const std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;l)
{
    if (l == list&lt;T&gt;())
    {
        return list&lt;T&gt;();
    }
    else
    {
        return add(l-&gt;head, copy(l-&gt;tail));
    }
}

template&lt;typename T&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; merge(const std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;a, const std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;b)
{
    if (a == list&lt;T&gt;())
    {
        return copy(b);
    }
    else
    {
        return add(a-&gt;head, merge(a-&gt;tail, b));
    }
}

template&lt;typename T, typename Filter&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; filter(const std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;l, Filter f)
{
    if (l == list&lt;T&gt;())
    {
        return list&lt;T&gt;();
    }
    else if (f(l-&gt;head))
    {
        return add(l-&gt;head, filter(l-&gt;tail, f));
    }
    else
    {
        return filter(l-&gt;tail, f);
    }
}

template&lt;typename T&gt;
std::unique_ptr&lt;List&lt;T&gt;&gt; sort(const std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;l)
{
    if (l == list&lt;T&gt;())
    {
        return list&lt;T&gt;();
    }
    else
    {
        return merge(sort(filter(l-&gt;tail, [&amp;](const T &amp;t){return t &lt; l-&gt;head;})), add(l-&gt;head, sort(filter(l-&gt;tail, [&amp;](const T &amp;t){return t &gt;= l-&gt;head;}))));
    }
}

template&lt;typename T&gt;
std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const std::unique_ptr&lt;List&lt;T&gt;&gt; &amp;l)
{
    if (l == list&lt;T&gt;())
    {
        return os;
    }
    else
    {
        return os &lt;&lt; l-&gt;head &lt;&lt; ' ' &lt;&lt; l-&gt;tail;
    }
}

std::unique_ptr&lt;List&lt;int&gt;&gt; randomList(int n)
{
    if (n == 0)
    {
        return list&lt;int&gt;();
    }
    else
    {
        return add(std::rand() % 100, randomList(n - 1));
    }
}

int main()
{
    auto r = randomList(100);
    std::cout &lt;&lt; r &lt;&lt; '\n' &lt;&lt; sort(r) &lt;&lt; '\n';
}
This looks pretty solid. I don't have any major comments. Just lots of minor ones. 



Unique or Shared

Why unique_ptr as a design choice over shared_ptr? The latter would let you be able to take shallow copies to your lists without having to have references everywhere. I'm not saying that shared_ptr is strictly better, more than it's something worth considering if you have not already. Either way, both are strictly better than raw pointers, so you're already on the right track. 

std::unique_ptr&lt;List&lt;T&gt;&gt;

That is a huge mouthful to type every time. And it is the only type that matters. So let's just alias it:

template &lt;typename T&gt;
using FList = std::unique_ptr&lt;List&lt;T&gt;&gt;;


Alternatively, rename List&lt;&gt; to Cons or Node so that the alias can be named List. To avoid confusion, I will use FList throughout the rest of this review.

cons()

The standard name for a list structure that is a value and a list is a "cons" cell. As in construct. Just for consistency, you should rename your add() to cons():

template&lt;typename T&gt;
FList&lt;T&gt; cons(const T&amp; t, FList&lt;T&gt;&amp;&amp; xs)
{
    return {new List&lt;T&gt;{t, std::move(xs)}};
}


Now, this is less usable than it could be, simply because you can't do something like:

FList&lt;long&gt; x = ...;
cons(4, std::move(x)); // error


For that, let's loosen the restrictions:

template &lt;typename T,
          typename U,
          typename = std::enable_if_t&lt;std::is_constructible&lt;T, U&amp;&amp;&gt;::value&gt;
          &gt;
FList&lt;T&gt; cons(U&amp;&amp; u, FList&lt;T&gt;&amp;&amp; xs)
{
    return {new List&lt;T&gt;{std::forward&lt;U&gt;(u), std::move(xs)}};
}


You'll note that I changed your variable from l to xs. 1 is a terrible variable name as it's very easy to mistake for l. So easy to mistake that you probably didn't notice that I wrote one the first time and el the second time. 

list()

Going on the theme of loosening restrictions, let's do that here too:

template&lt;typename T&gt;
FList&lt;T&gt; list()
{
    return {}; // shorter
}

template&lt;typename T, typename U, typename...Us&gt;
FList&lt;T&gt; list(U&amp;&amp; u, Us&amp;&amp;... us)
{
    return cons(std::forward&lt;U&gt;(u), 
                list&lt;T&gt;(std::forward&lt;Us&gt;(us)...)
                );
}


That will let you write:

auto nums = list&lt;int&gt;(1, '4', 5ull);


Checking for empty list

In your functions, you tend to start with:

if (l == list&lt;T&gt;())


But std::unique_ptr is usable in a boolean context as-is. So all of those checks you can write much simpler:

template&lt;typename T&gt;
FList&lt;T&gt; copy(const FList&lt;T&gt;&amp; xs)
{
    if (!xs)
    {
        return {};
    }
    else
    {
        return cons(xs-&gt;head, copy(xs-&gt;tail));
    }
}


Note that in the interest of being functional, we also probably want:

template &lt;typename T&gt;
T const&amp; car(FList&lt;T&gt; const&amp; xs) { // or T&amp; if you want mutability
    return xs-&gt;head;
}

template &lt;typename T&gt;
FList&lt;T&gt; const&amp; cdr(FList&lt;T&gt; const&amp; xs) { 
    return xs-&gt;tail;
}


So that this becomes:

return cons(car(xs), copy(cdr(xs)));


filter()

Could avoid some code duplication here:

template&lt;typename T, typename Filter&gt;
FList&lt;T&gt; filter(const FList&lt;T&gt;&amp; xs, Filter f)
{
    if (!xs) {
        return {};
    }

    auto tail = filter(cdr(xs), f);
    return f(car(xs)) 
           ? cons(car(xs), std::move(tail)) 
           : tail;
}


sort()

This function deserves a comment solely for:

    return merge(sort(filter(l-&gt;tail, [&amp;](const T &amp;t){return t &lt; l-&gt;head;})), add(l-&gt;head, sort(filter(l-&gt;tail, [&amp;](const T &amp;t){return t &gt;= l-&gt;head;}))));


It's like you never want anybody to read your code. So you're basically trying to do a sort of quicksort based on partitioning the head. Let's just write that in more than one line of code (and rename it to sorted to mimic python and make it sound less like you're sorting in place):

template&lt;typename T&gt;
FList&lt;T&gt; sorted(const FList&lt;T&gt;&amp; xs)
{
    if (!xs) return {};

    auto left = filter(cdr(xs), [&amp;](const T&amp; val){ return val &lt; car(xs); });
    auto right = filter(cdr(xs), [&amp;](const T&amp; val){ return !(val &lt; car(xs)); });

    return merge(sorted(left), 
                 cons(car(xs), sorted(right)));
}


I find that a ton easier to read. The reason I wrote the second filter with ! is that it naturally lends itself to adding a custom comparator:

template &lt;typename T, typename Comp = std::less&lt;T&gt;&gt;
FList&lt;T&gt; sorted(const FList&lt;T&gt;&amp; xs, Cmp cmp = {})
{
    if (!xs) return {};

    auto left = filter(cdr(xs), [&amp;](const T&amp; val){ return cmp(val, car(xs)); });
    auto right = filter(cdr(xs), [&amp;](const T&amp; val){ return !cmp(val, car(xs)); });

    return merge(sorted(left, cmp), 
                 cons(car(xs), sorted(right, cmp)));
}