4

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.

chris
  • 60,560
  • 13
  • 143
  • 205

2 Answers2

1

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 and std::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.

chris
  • 60,560
  • 13
  • 143
  • 205
  • Yes, nevermind, I checked the links and saw you don't use it as manipulator. Anyway, the compilation succeeds with non-console `std::ostream`: [LWS](http://liveworkspace.org/code/5c18ba3b03adec80676083b891c58156) – jrok Nov 01 '12 at 08:13
  • @jrok, Huh, I thought you couldn't create `std::ostream` objects, but apparently you can indirectly, so I guess not all objects of `std::ostream` can be assumed to be tied with the console. For what I'd use it for, it's probably fine, but that's a welcome improvement. – chris Nov 01 '12 at 08:37
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?

Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • Good thinking. Thanks for the `isatty` tip as well, and a big thanks for removing the variadic templates. I've not once seen variadic template template arguments used (if they're called that), and probably for good reason. I'm sure there's a decently easy Windows way of checking if stdout is CONOUT$ or something like that that can be used in conjunction with #ifdef if it goes cross-platform. – chris Nov 01 '12 at 09:29