How to print anything in C++ (part 2)

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript

We have a basic plan, and the ability to detect concrete types. But how can we detect whether an object supports output with operator<<? For this, there is a recently-discovered amazing trick. Here’s the code:

template <typename...>
using void_t = void;
 
template <typename T>
using operator_output_t = decltype(std::cout << std::declval<T>());
 
template <typename T, typename = void>
struct has_operator_output : public std::false_type {};
 
template <typename T>
struct has_operator_output<T, void_t<operator_output_t<T>>> 
  : public std::true_type {};

The key here is void_t. It’s a template alias that takes any template arguments, and always produces void. So what’s the use of that? Well, it doesn’t take just any old arguments. It can only work with well-formed types. So what happens if we give it a type that isn’t well-formed? Nothing too bad – because of course, Substitution Failure Is Not An Error.

Look at operator_output_t. It uses decltype to produce the type of outputting a T. If T doesn’t support operator<<, this type will be invalid.

Now look at has_operator_output. We specialize it using the result of void_t on operator_output_t<T>. If this is ill-formed (T can’t support operator<<) then substitution fails and the unspecialized template, std::false_type, is used. But if T supports operator<<, the result of void_t is well-formed. It’s still void, but it’s more specialized than the default template and so we get std::true_type!

That is the magic that void_t allows; a whole new frontier of template metaprogramming. (And if we relax the “because-we-can-be-general” variadic template, and convert the template aliases, it’s plain old C++98 specialization! Although it’s the combo with decltype and declval that really gives it phenomenal cosmic power.)

I’m going to set up a template to work on the tag types, so that the default implementation of stringifier in part 1 now becomes stringifier_select with a tag type that we can specialize on:

template <typename T, typename TAG>
struct stringifier_select;
 
template <typename T>
using stringifier = stringifier_select<T, stringifier_tag<T>>;
 
template <typename T, typename TAG>
struct stringifier_select
{
  explicit stringifier_select(const T&) {}
 
  std::ostream& output(std::ostream& s) const
  {
    return s << "<unknown>";
  }
};

And I can set up type discrimination for outputtable types, and implement their output function trivially:

template <typename T>
using stringifier_tag = std::conditional_t<
  has_operator_output<T>::value,
  is_outputtable_tag,
  void>;
 
template <typename T>
struct stringifier_select<T, is_outputtable_tag>
{
  explicit stringifier_select(const T& t) : m_t(t) {}
 
  std::ostream& output(std::ostream& s) const
  {
    return s << m_t;
  }
 
  const T& m_t;
};

So, now that we know the void_t pattern, it’s trivial to use it for detecting any container type; in fact, any type that has begin() and end().

template <typename T>
using begin_t = decltype(std::declval<T>().begin());
 
template <typename T, typename = void>
struct has_begin : public std::false_type {};
 
template <typename T>
struct has_begin<T, void_t<begin_t<T>>> 
  : public std::true_type {};
 
template <typename T>
using end_t = decltype(std::declval<T>().end());
 
template <typename T, typename = void>
struct has_end : public std::false_type {};
 
template <typename T>
struct has_end<T, void_t<end_t<T>>> 
  : public std::true_type {};

And then we have a very straightforward way to detect a container or other iterable type:

template<typename T>
using is_iterable = typename std::conditional<
  has_begin<T>::value && has_end<T>::value,
  std::true_type, std::false_type>::type;

To actually output a container, we will need to output surrounding braces and a comma in between each element. We might want to vary opening/closing/separating characters by container type, so I want to allow them to be specialized:

template <typename T>
struct iterable_opener
{
  constexpr const char* operator()(const T&) const
  { return "{"; }
};
 
template <typename T>
struct iterable_closer
{
  constexpr const char* operator()(const T&) const
  { return "}"; }
};
 
template <typename T>
struct iterable_separator
{
  constexpr const char* operator()(const T&) const
  { return ","; }
};

I originally implemented each of these as a straightforward const char*, but using functions and passing the container itself allows them to access (for example) size information which might be useful.

Now that we have the customization points in place, we can write the specialization of stringifier for containers. There’s a minor wrinkle to deal with fenceposting the separators.

template <typename T>
struct stringifier_select<T, is_iterable_tag>
{
  explicit stringifier_select(const T& t) : m_t(t) {}
 
  std::ostream& output(std::ostream& s) const
  {
    s << iterable_opener<T>()(m_t);
    auto b = m_t.begin();
    auto e = m_t.end();
    if (b != e)
      s << prettyprint(*b);
    std::for_each(++b, e,
                  [&s, this] (auto& e)
                  { s << iterable_separator<T>()(m_t)
                      << prettyprint(e); });
    return s << iterable_closer<T>()(m_t);
  }
  const T& m_t;
};

(Notice that it calls prettyprint to output each element recursively.) And that’s it, we can handle all iterable things. Next, handling pair and tuple.

Leave a Reply