问题
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.
回答1:
Here's a trait for detecting std::basic_ostream
instantiations:
template<typename T> struct is_basic_ostream {
template<typename U, typename V>
static char (&impl(std::basic_ostream<U, V> *))[
std::is_same<T, std::basic_ostream<U, V>>::value ? 2 : 1];
static char impl(...);
static constexpr bool value = sizeof(impl((T *)0)) == 2;
};
Use as:
template<typename T>
void foo(T &) {
static_assert(is_basic_ostream<T>::value,
"Argument must be of type std::basic_ostream<T, U>.");
}
We use template argument deduction to infer the template parameters on the (non-proper) basic_ostream
base class, if any. As a more general solution, replacing U
and V
with a single variadic parameter would allow writing a generic is_instantiation_of
trait 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?
回答2:
This is what I came up with after a lot of trial:
template<template<typename...> class T, typename... U>
void foo(T<U...> &os) {
static_assert(
std::is_same<
std::basic_ostream<U...>,
typename std::remove_reference<decltype(os)>::type
>::value,
"Argument must be of type std::basic_ostream<T, U>."
);
//...
}
Source code containing each of the below tests can be found here.
Source code replacing the types with similar self-made ones that are more explicit and offer more freedom (e.g., instantiation), which might be more useful for testing, can be found here.
- Passing in
std::cout
andstd::wcout
makes it compile fine. - Passing in an instance of
std::ostringstream
causes it to complain about the number of template arguments. - Passing in an instance of
std::fstream
, which has the same number of template parameters, causes the static assertion to fail. - Passing in a self-made 2-parameter template class causes the static assertion to fail.
Please feel free to improve upon this any way you can.
来源:https://stackoverflow.com/questions/13172649/ensure-argument-is-an-output-stream-for-the-console