Last active
March 19, 2017 18:27
-
-
Save hatsusato/a75990b89aacc9dee3a8a2a65ed67d5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <iostream> | |
using Manip = std::function<void(std::ostream&)>; | |
std::ostream& operator<<(std::ostream& os, Manip m) { | |
m(os); | |
return os; | |
} | |
template <typename T> | |
Manip paren(T&& x) { | |
return [x = std::forward<T>(x)](std::ostream& os) { | |
os << '(' << x << ')'; | |
}; | |
} | |
template <typename S> | |
Manip join(S) { | |
return [](std::ostream&){}; | |
} | |
template <typename S, typename T> | |
Manip join(S, T&& t) { | |
return [t = std::forward<T>(t)](std::ostream& os){ | |
os << t; | |
}; | |
} | |
template <typename S, typename T, typename U, typename... Args> | |
Manip join(S s, T&& t, U&& u, Args&&... args) { | |
return [s = s, t = std::forward<T>(t), | |
r = join(s, std::forward<U>(u), std::forward<Args>(args)...)] | |
(std::ostream& os){ | |
os << t << s << r; | |
}; | |
} | |
template <typename... Args> | |
Manip tuple(Args&&... args) { | |
return [t = join(", ", std::forward<Args>(args)...)](std::ostream& os){ | |
os << paren(t); | |
}; | |
} | |
int main() { | |
std::cout << tuple() << std::endl; | |
std::cout << tuple(0) << std::endl; | |
std::cout << tuple(1.2, 3.4, 5.6, 7.8) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment