#include <iostream>
#include <vector>
namespace manips
{
template <typename Cont, typename Delim=const char*>
struct PrintManip {
PrintManip(Cont const& v, Delim d = ", ") : _v(v), _d(std::move(d)) { }
Cont const& _v;
Delim _d;
friend std::ostream& operator<<(std::ostream& os, PrintManip const& manip) {
using namespace std;
auto f = begin(manip._v), l(end(manip._v));
os << "{ ";
while (f != l)
if ((os << *f) && (++f != l))
os << manip._d;
return os << " }";
}
};
template <typename T, typename Delim=const char*>
manips::PrintManip<T, Delim> print(T const& deduce, Delim delim = ", ") {
return { deduce, std::move(delim) };
}
}
using manips::print;
int main()
{
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even: " << print(std::vector<int> { 1,2,3,42 }, ':') << "!\n"
<< "or bizarrely: " << print(arr, print(arr)) << "\n";
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKbmFtZXNwYWNlIG1hbmlwcwp7CiAgICB0ZW1wbGF0ZSA8dHlwZW5hbWUgQ29udCwgdHlwZW5hbWUgRGVsaW09Y29uc3QgY2hhcio+CiAgICBzdHJ1Y3QgUHJpbnRNYW5pcCB7IAogICAgICAgIFByaW50TWFuaXAoQ29udCBjb25zdCYgdiwgRGVsaW0gZCA9ICIsICIpIDogX3YodiksIF9kKHN0ZDo6bW92ZShkKSkgeyB9CgogICAgICAgIENvbnQgY29uc3QmIF92OwogICAgICAgIERlbGltIF9kOwoKICAgICAgICBmcmllbmQgc3RkOjpvc3RyZWFtJiBvcGVyYXRvcjw8KHN0ZDo6b3N0cmVhbSYgb3MsIFByaW50TWFuaXAgY29uc3QmIG1hbmlwKSB7CiAgICAgICAgICAgIHVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAgICAgICAgICAgIGF1dG8gZiA9IGJlZ2luKG1hbmlwLl92KSwgbChlbmQobWFuaXAuX3YpKTsgCgogICAgICAgICAgICBvcyA8PCAieyAiOwogICAgICAgICAgICB3aGlsZSAoZiAhPSBsKQogIC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