overloaded.h 709 B

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. namespace c10 {
  3. namespace detail {
  4. template <class... Ts>
  5. struct overloaded_t {};
  6. template <class T0>
  7. struct overloaded_t<T0> : T0 {
  8. using T0::operator();
  9. overloaded_t(T0 t0) : T0(std::move(t0)) {}
  10. };
  11. template <class T0, class... Ts>
  12. struct overloaded_t<T0, Ts...> : T0, overloaded_t<Ts...> {
  13. using T0::operator();
  14. using overloaded_t<Ts...>::operator();
  15. overloaded_t(T0 t0, Ts... ts)
  16. : T0(std::move(t0)), overloaded_t<Ts...>(std::move(ts)...) {}
  17. };
  18. } // namespace detail
  19. // Construct an overloaded callable combining multiple callables, e.g. lambdas
  20. template <class... Ts>
  21. detail::overloaded_t<Ts...> overloaded(Ts... ts) {
  22. return {std::move(ts)...};
  23. }
  24. } // namespace c10