An output manipulator is simply any type for which os << m
is defined for some basic_ostream
instantiation. A manipulator can be a function (subject to the operator<<
overloads of basic_ostream
) but it can also be any type which defines its own operator<<
. As such we need to perform type erasure to capture the operator<<
for an appropriate basic_ostream
instantiation; the simplest way to do this is with std::function
and a lambda:
#include <iostream>
#include <iomanip>
#include <functional>
#include <vector>
template<typename S>
struct out_manipulator: public std::function<S &(S &)> {
template<typename T> out_manipulator(T &&t): std::function<S &(S &)>(
[=](S &i) -> S &{ return i << t; }) {}
template<typename T> out_manipulator(T *t): std::function<S &(S &)>(
[=](S &i) -> S &{ return i << t; }) {} // for g++
template<typename U> friend U &operator<<(U &u, out_manipulator &a) {
return static_cast<U &>(a(u));
}
};
void print(const std::vector<out_manipulator<std::ostream>> &manips) {
for (auto m: manips)
std::cout << m;
}
int main() {
print({std::fixed, std::setprecision(2)});
std::cout << 3.14159;
}