Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created June 8, 2019 19:12
Show Gist options
  • Save dgodfrey206/e1a649d61e1e025cf0791203e8487ae3 to your computer and use it in GitHub Desktop.
Save dgodfrey206/e1a649d61e1e025cf0791203e8487ae3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
template<class T>
struct sticky_streamer_t {
std::ostream* os = nullptr;
T x;
int order;
sticky_streamer_t()=default;
sticky_streamer_t(sticky_streamer_t const&)=default;
sticky_streamer_t(std::ostream* os, T&& x, int order = 1) : os(os), x(x), order(order) { }
sticky_streamer_t(sticky_streamer_t&& ss) : os(ss.os), x(ss.x), order(ss.order) { ss.os = nullptr; }
template<class U>
sticky_streamer_t operator<<(U&& u) & {
if (!os) return *this;
if (order == 1) (*os) << forward<U>(u) << x;
else (*os) << x << forward<U>(u);
return *this;
}
template<class U>
sticky_streamer_t operator<<(U&& u) && {
return std::move(*this << forward<U>(u));
}
};
template<class T>
sticky_streamer_t<T> operator|(std::ostream& os, T&& x) {
return {&os, forward<T>(x), 1};
}
template<class T>
sticky_streamer_t<T> operator|(T&& x, std::ostream& os) {
return {&os, forward<T>(x), 2};
}
int main() {
('\n' | std::cout) << 1 << 2 << 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment