Removing magic constant from template param check #3
@ -48,11 +48,13 @@ The integer/string conversion is done using a simple method I learned over the y
|
|||||||
(*Note: The below examples of code are not up-to-date, though they still give a general idea of how `to_string` works.*)
|
(*Note: The below examples of code are not up-to-date, though they still give a general idea of how `to_string` works.*)
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
|
||||||
constexpr to_string_t() {
|
constexpr to_string_t() {
|
||||||
auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
|
auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
|
||||||
*--ptr = '\0';
|
*--ptr = '\0';
|
||||||
for (auto n = N < 0 ? -N : N; n; n /= base)
|
for (auto n = N < 0 ? -N : N; n; n /= base)
|
||||||
*--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n % base];
|
*--ptr = digits[n % base];
|
||||||
if (N < 0)
|
if (N < 0)
|
||||||
*--ptr = '-';
|
*--ptr = '-';
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,9 @@
|
|||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
constexpr auto digit_count = sizeof(digits) / sizeof(digits[0]);
|
||||||
|
|
||||||
|
|||||||
/**
|
/**
|
||||||
![]() also also `inline constexpr`
|
|||||||
* @struct to_string_t
|
* @struct to_string_t
|
||||||
* @brief Provides the ability to convert any integral to a string at compile-time.
|
* @brief Provides the ability to convert any integral to a string at compile-time.
|
||||||
@ -17,7 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
template<auto N, unsigned int base, typename char_type,
|
template<auto N, unsigned int base, typename char_type,
|
||||||
std::enable_if_t<std::is_integral_v<decltype(N)>, int> = 0,
|
std::enable_if_t<std::is_integral_v<decltype(N)>, int> = 0,
|
||||||
std::enable_if_t<(base > 1 && base < 37), int> = 0>
|
std::enable_if_t<(base > 1 && base < digit_count), int> = 0>
|
||||||
struct to_string_t {
|
struct to_string_t {
|
||||||
// The lambda calculates what the string length of N will be, so that `buf`
|
// The lambda calculates what the string length of N will be, so that `buf`
|
||||||
// fits to the number perfectly.
|
// fits to the number perfectly.
|
||||||
@ -35,7 +38,7 @@ struct to_string_t {
|
|||||||
auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
|
auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
|
||||||
*--ptr = '\0';
|
*--ptr = '\0';
|
||||||
for (auto n = N < 0 ? -N : N; n; n /= base)
|
for (auto n = N < 0 ? -N : N; n; n /= base)
|
||||||
*--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n % base];
|
*--ptr = digits[n % base];
|
||||||
if (N < 0)
|
if (N < 0)
|
||||||
*--ptr = '-';
|
*--ptr = '-';
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user
inline constexpr
@Neargye why
inline constexpr
whenconstexpr
itself is implicitly inline?constexpr specifier implies inline for static data members as well as functions, but not for global variable.
https://en.cppreference.com/w/cpp/language/inline