nth.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/functional/arg.hpp>
  5. #include <cstddef>
  6. #include <utility>
  7. namespace hana = boost::hana;
  8. constexpr int to_int(char c)
  9. { return static_cast<int>(c) - 48; }
  10. template <std::size_t N>
  11. constexpr long long parse(const char (&arr)[N]) {
  12. long long number = 0, base = 1;
  13. for (std::size_t i = 0; i < N; ++i) {
  14. number += to_int(arr[N - 1 - i]) * base;
  15. base *= 10;
  16. }
  17. return number;
  18. }
  19. template <char ...c>
  20. struct pick {
  21. static constexpr unsigned long n = parse<sizeof...(c)>({c...});
  22. template <typename ...T>
  23. constexpr auto operator()(T&& ...args) const
  24. { return hana::arg<n>(std::forward<T>(args)...); }
  25. };
  26. template <char ...c> constexpr pick<c...> operator"" _st() { return {}; }
  27. template <char ...c> constexpr pick<c...> operator"" _nd() { return {}; }
  28. template <char ...c> constexpr pick<c...> operator"" _rd() { return {}; }
  29. template <char ...c> constexpr pick<c...> operator"" _th() { return {}; }
  30. static_assert(1_st(1, '2', 3.3, 444) == 1, "");
  31. static_assert(2_nd(1, '2', 3.3, 444) == '2', "");
  32. static_assert(3_rd(1, '2', 3.3, 444) == 3.3, "");
  33. static_assert(4_th(1, '2', 3.3, 444) == 444, "");
  34. int main() { }