Skip to content

Commit b2524e1

Browse files
committed
Add type aliases and literals up to uint512
Fill the gap in the type aliases and literas in the range 128-512. Also place the literal operators in the intx::literals namespace to allow using the literals without `using namespace intx`.
1 parent 394c5bc commit b2524e1

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

include/intx/intx.hpp

+26-15
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,10 @@ inline constexpr Int from_string(const std::string& s)
916916
return from_string<Int>(s.c_str());
917917
}
918918

919-
consteval uint128 operator""_u128(const char* s)
920-
{
921-
return from_string<uint128>(s);
922-
}
919+
// consteval uint128 operator""_u128(const char* s)
920+
// {
921+
// return from_string<uint128>(s);
922+
// }
923923

924924
template <unsigned N>
925925
inline std::string to_string(uint<N> x, int base = 10)
@@ -1104,6 +1104,7 @@ using uint192 = uint<192>;
11041104
using uint256 = uint<256>;
11051105
using uint320 = uint<320>;
11061106
using uint384 = uint<384>;
1107+
using uint448 = uint<448>;
11071108
using uint512 = uint<512>;
11081109

11091110
template <unsigned N>
@@ -1843,17 +1844,27 @@ inline constexpr uint256 mulmod(const uint256& x, const uint256& y, const uint25
18431844
return udivrem(umul(x, y), mod).rem;
18441845
}
18451846

1846-
1847-
consteval uint256 operator""_u256(const char* s)
1848-
{
1849-
return from_string<uint256>(s);
1850-
}
1851-
1852-
consteval uint512 operator""_u512(const char* s)
1853-
{
1854-
return from_string<uint512>(s);
1855-
}
1856-
1847+
/// Define type alias uintN = uint<N> and the matching literal ""_uN.
1848+
/// The literal operators are defined in the intx::literals namespace.
1849+
#define DEFINE_ALIAS_AND_LITERAL(N) \
1850+
using uint##N = uint<N>; \
1851+
namespace literals \
1852+
{ \
1853+
consteval uint##N operator""_u##N(const char* s) \
1854+
{ \
1855+
return from_string<uint##N>(s); \
1856+
} \
1857+
}
1858+
DEFINE_ALIAS_AND_LITERAL(128);
1859+
DEFINE_ALIAS_AND_LITERAL(192);
1860+
DEFINE_ALIAS_AND_LITERAL(256);
1861+
DEFINE_ALIAS_AND_LITERAL(320);
1862+
DEFINE_ALIAS_AND_LITERAL(384);
1863+
DEFINE_ALIAS_AND_LITERAL(448);
1864+
DEFINE_ALIAS_AND_LITERAL(512);
1865+
#undef DEFINE_ALIAS_AND_LITERAL
1866+
1867+
using namespace literals;
18571868

18581869
/// Convert native representation to/from little-endian byte order.
18591870
/// intx and built-in integral types are supported.

test/unittests/test_intx_api.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
#include "test_suite.hpp"
66

7+
using namespace intx::literals;
8+
9+
static_assert(1_u128);
10+
static_assert(1_u192);
11+
static_assert(1_u256);
12+
static_assert(1_u320);
13+
static_assert(1_u384);
14+
static_assert(1_u448);
15+
static_assert(1_u512);
16+
717
using namespace intx;
818

919
static_assert(uint128{2} + uint128{2} == 4);

0 commit comments

Comments
 (0)