Efficient Implementation of functional and Lazy evaluation in C++
I think this question may be off-topic, as it does not consist of complete working code to be reviewed. I spent (too long) trying to get it to compile, and got as far as this: https://godbolt.org/z/qCE9S8

But you have a lot of problems with the current design. Most notably, you're using raw new all over the place, which creates pointers to the heap; but you aren't actually using the correct syntax to refer to those pointers. For example:

Stream&lt;N&gt; map(function&lt;N(T)&gt; mapper) {
    return new MapStream&lt;N,T&gt;(this, mapper);
}


Here new MapStream&lt;...&gt;() yields a value of type MapStream&lt;N,T&gt;*, but you're trying to return it as if it were a Stream&lt;N&gt; object. This flatly will not compile.

You could consider changing this return type to std::unique_ptr&lt;Stream&lt;N&gt;&gt; (and using make_unique), but that still won't really work for your use-case, because then you'll have to change this line:

stream.map(hash1).map(hash2).foreach(summer);


to:

stream.map(hash1)-&gt;map(hash2)-&gt;foreach(summer);


because now map returns a pointer. And worse, there's no way for the MapStream object itself to transfer its own ownership into the previous_ member of the next MapStream object in the chain. You end up with a bunch of temporary unique_ptrs, all linked together by raw pointers which will dangle as soon as the current full-expression finishes. That's okay for your benchmark, but it won't work at all in practice.

You might consider looking at a type erasure design, so that you could keep using Stream&lt;int&gt; as a value type (not a polymorphic base class, no visible pointers) but give it behavior that appeared polymorphic at run time.