fork(5) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sstream>
  4. #include <type_traits>
  5.  
  6. template<typename S, typename T>
  7. class is_streamable
  8. {
  9. template<typename SS, typename TT>
  10. static auto test(SS&& s, TT&& t) -> decltype(s << t);
  11.  
  12. struct dummy_t {};
  13. static dummy_t test(...);
  14.  
  15. using return_type = decltype(test(std::declval<S>(), std::declval<T>()));
  16.  
  17. public:
  18. static const bool value = !std::is_same<return_type, dummy_t>::value;
  19. };
  20.  
  21. class C {};
  22.  
  23. class D {};
  24.  
  25. std::ostream& operator <<(std::ostream&, D const&);
  26.  
  27. int main() {
  28. std::cout << std::boolalpha;
  29. std::cout <<is_streamable<std::stringstream, C>::value << '\n';
  30. std::cout <<is_streamable<std::stringstream, D>::value << '\n';
  31. return 0;
  32. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
false
true