Loading...
Searching...
No Matches
to_string_literal.h
Go to the documentation of this file.
1#ifndef _BBM_TO_STRING_LITERAL_H_
2#define _BBM_TO_STRING_LITERAL_H_
3
4#include <concepts>
5#include <cmath>
7
8/***********************************************************************/
9/*! \file to_string_literal.h
10 \brief convert other types to a string literal.
11************************************************************************/
12
13namespace bbm {
14
15 /*********************************************************************/
16 /*! \brief convert integrals to string_literal
17
18 \tparam N = (integer) literal to covert into a string_literal
19 \tparam base = conversion base; must be less than 16. Default base=10.
20 *********************************************************************/
21 template<auto N, int base=10> requires std::integral<decltype(N)> && (base <= 16)
22 constexpr auto to_string_literal(void)
23 {
24 // convert digit to chars
25 constexpr char digits[17] = "0123456789ABCDEF";
26
27 // if negative, add '-' sign
28 if constexpr (N < 0) return string_literal("-") + to_string_literal<-N, base>();
29
30 // if less than base; end recursion
31 else if constexpr (N < base) return string_literal<2>({digits[N], '\0'});
32
33 // otherwise, convert last digit, and recurse on N/base
34 else return to_string_literal<N / base, base>() + to_string_literal<N % base, base>();
35 }
36
37} // end bbm namespace
38
39#endif /* _BBM_TO_STRING_LITERAL_H_ */
Definition: aggregatebsdf.h:29
constexpr auto to_string_literal(void)
convert integrals to string_literal
Definition: to_string_literal.h:22
Definition: string_literal.h:16