• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include <boost/hana/functional/arg.hpp>
6 
7 #include <cstddef>
8 #include <utility>
9 namespace hana = boost::hana;
10 
11 
to_int(char c)12 constexpr int to_int(char c)
13 { return static_cast<int>(c) - 48; }
14 
15 template <std::size_t N>
parse(const char (& arr)[N])16 constexpr long long parse(const char (&arr)[N]) {
17     long long number = 0, base = 1;
18     for (std::size_t i = 0; i < N; ++i) {
19         number += to_int(arr[N - 1 - i]) * base;
20         base *= 10;
21     }
22     return number;
23 }
24 
25 template <char ...c>
26 struct pick {
27     static constexpr unsigned long n = parse<sizeof...(c)>({c...});
28 
29     template <typename ...T>
operator ()pick30     constexpr auto operator()(T&& ...args) const
31     { return hana::arg<n>(std::forward<T>(args)...); }
32 };
33 
operator ""_st()34 template <char ...c> constexpr pick<c...> operator"" _st() { return {}; }
operator ""_nd()35 template <char ...c> constexpr pick<c...> operator"" _nd() { return {}; }
operator ""_rd()36 template <char ...c> constexpr pick<c...> operator"" _rd() { return {}; }
operator ""_th()37 template <char ...c> constexpr pick<c...> operator"" _th() { return {}; }
38 
39 
40 static_assert(1_st(1, '2', 3.3, 444) == 1, "");
41 static_assert(2_nd(1, '2', 3.3, 444) == '2', "");
42 static_assert(3_rd(1, '2', 3.3, 444) == 3.3, "");
43 static_assert(4_th(1, '2', 3.3, 444) == 444, "");
44 
main()45 int main() { }
46