GMP 0.3.0
Generative Metaprogramming library for C++
Loading...
Searching...
No Matches
to_fixed_string.hpp
Go to the documentation of this file.
1// ___ __ __ ___
2// / __| \/ | _ \ GMP(Generative Metaprogramming)
3// | (_ | |\/| | _/ version 0.3.0
4// \___|_| |_|_| https://github.com/lkimuk/gmp
5//
6// SPDX-FileCopyrightText: 2026 Miles Li <https://www.cppmore.com/>
7// SPDX-License-Identifier: MIT
8//
9// This file is part of the GMP (Generative Metaprogramming) library.
10// Full project source: https://github.com/lkimuk/gmp
11
12#ifndef GMP_META_TO_FIXED_STRING_HPP_
13#define GMP_META_TO_FIXED_STRING_HPP_
14
15#include <cstdint>
17
18namespace gmp {
19
20namespace detail {
21
22template<std::intmax_t N>
23struct to_fixed_string_impl {
24 static constexpr auto value = []() {
25 constexpr auto buflen = []() {
26 unsigned int len = (N == 0) ? 1 : (N > 0 ? 1 : 2);
27 for (auto n = N; n; len++, n /= 10);
28 return len;
29 }();
30
31 char buf[buflen] = {};
32 auto ptr = buf + buflen;
33 *--ptr = '\0';
34
35 if constexpr (N != 0) {
36 auto n = N < 0 ? -N : N;
37 for (; n; n /= 10)
38 *--ptr = "0123456789"[n % 10];
39 if constexpr (N < 0)
40 *--ptr = '-';
41 } else {
42 buf[0] = '0';
43 }
44
45 return fixed_string<buflen - 1>(buf);
46 }();
47};
48
49} // namespace detail
50
55template<std::intmax_t N>
56static constexpr auto to_fixed_string_v = detail::to_fixed_string_impl<N>::value;
57
60} // namespace gmp
61
62#endif // GMP_META_TO_FIXED_STRING_HPP_
static constexpr auto to_fixed_string_v
fixed_string(char const(&)[N]) -> fixed_string< N - 1 >
Deduce fixed_string length from a string literal.
consteval auto enum_values()
Get all enumerator values of an enumeration type at compile-time.
Definition meta.hpp:155
Definition lock.hpp:21