I'd like to create a static lookup table for powers of ten returned as type boost::mulitprecision's uint256_t
:
static uint256_t power_ten(const uint8_t n)
{
static const uint256_t table[] =
{
1,
10
// etc
};
return table[n];
}
However, I won't be able to define the literals for 256 bits because C++ literals are int
s.
Consequently I thought I could use template metaprogramming to calculate the powers so I don't need to specify literals, similar to this Fibonacci example I found:
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using Integer = int; // Works
//using Integer = uint256_t; // Causes compiler errors
template<Integer n>
struct fibonacci
{
static constexpr Integer value = fibonacci<n-1>::value + fibonacci<n-2>::value;
};
template<>
struct fibonacci<0>
{
static constexpr Integer value = 0;
};
template<>
struct fibonacci<1>
{
static constexpr Integer value = 1;
};
int main()
{
Integer a = fibonacci<40>::value;
std::cout << a << std::endl;
}
This code works for int
. However, if I comment/uncomment to use uint256_t
I get these compiler errors:
clang-14: warning: -lquadmath: 'linker' input unused [-Wunused-command-line-argument]
<source>:8:18: error: a non-type template parameter cannot have type 'Integer' (aka 'number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void>>')
template<Integer n>
^
<source>:11:28: error: declaration of constexpr static data member 'value' requires an initializer
static constexpr Integer value = fibonacci<n-1>::value + fibonacci<n-2>::value;
^
2 errors generated.
ASM generation compiler returned: 1
<source>:8:18: error: a non-type template parameter cannot have type 'Integer' (aka 'number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void>>')
template<Integer n>
^
<source>:11:28: error: declaration of constexpr static data member 'value' requires an initializer
static constexpr Integer value = fibonacci<n-1>::value + fibonacci<n-2>::value;
https://godbolt.org/z/qsK8rG5WG
Is there another way to achieve this?
UPDATE
I've just tried populating the static table using uint256_t
with a string constructor, as per the comments. This seemed to work but once I reach:
uint256_t("1000000000000000000000000000000000000000000000000000000000000000000000000000000")
std::cout
outputs the wrong value:
73663286101470436611432119930496737173840122674875487684339327936694962880512