I’m trying to make a stream manipulator for colour for use with output to the console. It works, changing the colour of text and the background:
std::cout << ConColor::Color::FgBlue << 123 << "abc"; //text is blue, sticky
The problem is with the signature:
std::ostream &FgBlue(std::ostream &);
This signature allows for derived classes, such as std::ostringstream as well, but there is no way to change the colour of a string stream. The function would change the colour of the console regardless if it was called with such an argument.
Therefore, I want to ensure the argument is something along the lines of std::cout, std::wcout, etc. I would prefer it be general in the case that more std::ostream objects are added in a future standard.
I tried many things involving std::is_same and std::is_base_of, when the former wouldn’t work, just to eventually realize that it was pointless because any argument type inheriting from std::basic_ostream<> will be casted to the type I’m comparing against when passed to the function, giving false positives.
This eventually led me to my answer below (variadic template template arguments? Wow, that’s a mouthful!) There are a couple problems, however:
- The compiler must support variadic templates. I would prefer the solution work on MSVC.
- The compiler gives cryptic errors in the case that a derived class with a different number of template arguments (such as
std::ostringstream, which has 3 instead of 2) is used, as it doesn’t get past the function signature. - It’s possible to redirect stdout, say, to a file, so even if the argument is
std::cout, the same thing as the stringstream case happens.
I encourage people to post any other solutions, hopefully better than mine, and really hopefully something that works with at least VS11.
Here’s a trait for detecting
std::basic_ostreaminstantiations:Use as:
We use template argument deduction to infer the template parameters on the (non-proper)
basic_ostreambase class, if any. As a more general solution, replacingUandVwith a single variadic parameter would allow writing a genericis_instantiation_oftrait on compilers that support variadic template parameters.To detect whether stdout is piped to a file (which can only be detected at runtime, of course) use
isatty; see how to use isatty() on cout, or can I assume that cout == file descriptor 1?