static_if.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2018-2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_DETAIL_STATIC_IF_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_STATIC_IF_HPP
  8. #include <type_traits>
  9. #include <utility>
  10. namespace boost {
  11. namespace histogram {
  12. namespace detail {
  13. template <class T, class F, class... Args>
  14. constexpr decltype(auto) static_if_impl(
  15. std::true_type, T&& t, F&&,
  16. Args&&... args) noexcept(noexcept(std::declval<T>()(std::declval<Args>()...))) {
  17. return std::forward<T>(t)(std::forward<Args>(args)...);
  18. }
  19. template <class T, class F, class... Args>
  20. constexpr decltype(auto) static_if_impl(
  21. std::false_type, T&&, F&& f,
  22. Args&&... args) noexcept(noexcept(std::declval<F>()(std::declval<Args>()...))) {
  23. return std::forward<F>(f)(std::forward<Args>(args)...);
  24. }
  25. template <bool B, class... Ts>
  26. constexpr decltype(auto) static_if_c(Ts&&... ts) noexcept(
  27. noexcept(static_if_impl(std::integral_constant<bool, B>{}, std::declval<Ts>()...))) {
  28. return static_if_impl(std::integral_constant<bool, B>{}, std::forward<Ts>(ts)...);
  29. }
  30. template <class Bool, class... Ts>
  31. constexpr decltype(auto) static_if(Ts&&... ts) noexcept(
  32. noexcept(static_if_impl(Bool{}, std::declval<Ts>()...))) {
  33. return static_if_impl(Bool{}, std::forward<Ts>(ts)...);
  34. }
  35. } // namespace detail
  36. } // namespace histogram
  37. } // namespace boost
  38. #endif