Functional pipeline in C++11

c++11 pipeline

I stumbled upon this nice blog post – pipable functions in C++14. This is realy fun idea as its usage plain for anybody who is familiar with unix pipelines. So i tried to use it in C++11 (without boost) from slightly different angle to make it more real-life concept.

First sample (fun, but not so interesting):

Second sample (functional style array processing):

Simple and short implementation is under the cut…

To implement the first sample  you need only the following short number of lines (C++11 without boost):

You can see a lot of C++11 features here coupled together – perfect forwarding, &&, std::bind, auto and decltype. Using this you can even define functions inside pipeline:

For second sample we need to define some extension functions. We don’t need linq library to make things work though. Let’s do it generic way:

Processing functions here are just example and not so suitable for large amount of data – but whole concept is still viable.

Last two functions are used to create pipable functors from static methods:

As you can see the concept requires very compact implementation. If you somehow applied this in real project plz write in comments.

  • Some Onee

    set is already a typename, you should not use it as a variable name especially you seem to use using namespace std;

    • Victor Laskin

      ok, i changed it to ‘numbers’

  • C++14 return type deduction would turn the long prototypes less intimidating.

    • Victor Laskin

      sure. but note that the most long prototypes here are the same for all logic and have to be written only once

  • Piers Powlesland

    Hi Victor, I’m a bit of a noob when it comes to c++ so apologies if this is a really stupid question.

    With this approach can you chain together functions with different input and output types. For example (int,string) | (string,bool) | (bool, void)

    • Victor Laskin

      Yes, you can.