overloaded.h 981 B

123456789101112131415161718192021222324252627282930313233343536
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <memory>
  4. namespace c10 {
  5. namespace detail {
  6. template <class... Ts>
  7. struct overloaded_t {};
  8. template <class T0>
  9. struct overloaded_t<T0> : T0 {
  10. using T0::operator();
  11. overloaded_t(T0 t0) : T0(std::move(t0)) {}
  12. };
  13. template <class T0, class... Ts>
  14. struct overloaded_t<T0, Ts...> : T0, overloaded_t<Ts...> {
  15. using T0::operator();
  16. using overloaded_t<Ts...>::operator();
  17. overloaded_t(T0 t0, Ts... ts)
  18. : T0(std::move(t0)), overloaded_t<Ts...>(std::move(ts)...) {}
  19. };
  20. } // namespace detail
  21. // Construct an overloaded callable combining multiple callables, e.g. lambdas
  22. template <class... Ts>
  23. detail::overloaded_t<Ts...> overloaded(Ts... ts) {
  24. return {std::move(ts)...};
  25. }
  26. } // namespace c10
  27. #else
  28. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  29. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)