/prelude/delimited_ostream.hpp

http://github.com/Eelis/geordi · C++ Header · 48 lines · 36 code · 12 blank · 0 comment · 1 complexity · 4c5cec86435f94bbb17e228e837918ac MD5 · raw file

  1. #ifndef DELIMITED_OSTREAM_HPP
  2. #define DELIMITED_OSTREAM_HPP
  3. #include <iostream>
  4. #include <ios>
  5. #if __cplusplus >= 201103
  6. #include <type_traits>
  7. namespace del_ostream_detail
  8. {
  9. template<typename T> constexpr bool take_by_value()
  10. { return std::is_integral<T>::value || std::is_enum<T>::value; }
  11. }
  12. template <typename Ch, typename Tr, typename T>
  13. inline typename std::enable_if<
  14. !del_ostream_detail::take_by_value<T>(),
  15. std::basic_ostream<Ch, Tr> >::type &
  16. operator,(std::basic_ostream<Ch, Tr> & o, T const & t)
  17. { return o << ", " << t; }
  18. template <typename Ch, typename Tr, typename T>
  19. inline typename std::enable_if<
  20. del_ostream_detail::take_by_value<T>(),
  21. std::basic_ostream<Ch, Tr> >::type &
  22. operator,(std::basic_ostream<Ch, Tr> & o, T const t)
  23. { return o << ", " << t; }
  24. #else
  25. template <typename Ch, typename Tr, typename T>
  26. inline std::basic_ostream<Ch, Tr> &
  27. operator,(std::basic_ostream<Ch, Tr> & o, T const & t)
  28. { return o << ", " << t; }
  29. #endif
  30. template <typename Ch, typename Tr>
  31. std::basic_ostream<Ch, Tr> & operator, (std::basic_ostream<Ch, Tr> & o, std::ios_base & (* const f) (std::ios_base &))
  32. { return o << f; }
  33. template <typename Ch, typename Tr>
  34. std::basic_ostream<Ch, Tr> & operator, (std::basic_ostream<Ch, Tr> & o, std::basic_ostream<Ch, Tr> & (* const f) (std::basic_ostream<Ch, Tr> &))
  35. { return o << f; }
  36. #endif // header guard