ostream.h 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef COMMON_OSTREAM_H_
  5. #define COMMON_OSTREAM_H_
  6. #include "llvm/Support/raw_ostream.h"
  7. namespace Carbon {
  8. // Support ostream << for types which implement:
  9. // void Print(llvm::raw_ostream& out) const;
  10. template <typename T, typename std::enable_if<std::is_member_function_pointer<
  11. decltype(&T::Print)>::value>::type* = nullptr>
  12. auto operator<<(llvm::raw_ostream& out, const T& obj) -> llvm::raw_ostream& {
  13. obj.Print(out);
  14. return out;
  15. }
  16. // Prevents ostream << for pointers to printable types.
  17. template <typename T, typename std::enable_if<std::is_member_function_pointer<
  18. decltype(&T::Print)>::value>::type* = nullptr>
  19. __attribute__((unavailable(
  20. "Received a pointer to a printable type, are you missing a `*`? "
  21. "To print as a pointer, cast to void*."))) auto
  22. operator<<(llvm::raw_ostream& out, const T* /*obj*/) -> llvm::raw_ostream&;
  23. } // namespace Carbon
  24. #endif // COMMON_OSTREAM_H_