1// Copyright Louis Dionne 2013-2022
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>
9namespace hana = boost::hana;
10
11
12constexpr int to_int(char c)
13{ return static_cast<int>(c) - 48; }
14
15template <std::size_t N>
16constexpr 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
25template <char ...c>
26struct pick {
27 static constexpr unsigned long n = parse<sizeof...(c)>({c...});
28
29 template <typename ...T>
30 constexpr auto operator()(T&& ...args) const
31 { return hana::arg<n>(std::forward<T>(args)...); }
32};
33
34template <char ...c> constexpr pick<c...> operator"" _st() { return {}; }
35template <char ...c> constexpr pick<c...> operator"" _nd() { return {}; }
36template <char ...c> constexpr pick<c...> operator"" _rd() { return {}; }
37template <char ...c> constexpr pick<c...> operator"" _th() { return {}; }
38
39
40static_assert(1_st(1, '2', 3.3, 444) == 1, "");
41static_assert(2_nd(1, '2', 3.3, 444) == '2', "");
42static_assert(3_rd(1, '2', 3.3, 444) == 3.3, "");
43static_assert(4_th(1, '2', 3.3, 444) == 444, "");
44
45int main() { }
46

source code of boost/libs/hana/example/misc/nth.cpp