Skip to content

Instantly share code, notes, and snippets.

@SealtielFreak
Created May 28, 2022 16:42
Show Gist options
  • Save SealtielFreak/683658f5d3cd9182c529078fe853eb93 to your computer and use it in GitHub Desktop.
Save SealtielFreak/683658f5d3cd9182c529078fe853eb93 to your computer and use it in GitHub Desktop.
Pipes in C++
template<typename T>
class Pipe {
T m_value;
public:
Pipe(const T &value) : m_value(value) { /**/ }
Pipe<T> operator |(const std::function<T(T)> &func) {
return func(m_value);
}
explicit operator T() const {
return m_value;
}
};
/*
// Example usage:
int foo(int i) {
return i + 3;
}
int bzr(int i) {
return ++i;
}
int main() {
auto pipe = Pipe(0) | foo
| bzr;
int i = (int) pipe; // output: 4
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment