On cppreference there is a mentioning that one can have templated user-literal operators, with some restrictions:
If the literal operator is a template, it must have an empty parameter list and can have only one template parameter, which must be a non-type template parameter pack with element type
char
, such as
template <char...> double operator "" _x();
So I wrote one like in the code below:
template <char...>
double operator "" _x()
{
return .42;
}
int main()
{
10_x; // empty template list, how to specify non-empty template parameters?
}
Question:
10_x<'a'>;
or 10_<'a'>x;
does not compile.10_x; // empty template list, how to specify non-empty template parameters?
That isn't quite right. The template parameter list isn't empty. When you write:
template <char... Cs>
??? operator "" _x()
The Cs
get populated from the stuff on the left-hand side of the literal. That is, when you write:
10_x
that calls:
operator ""_x<'1', '0'>();
One simple example would be to build a compile time, overflow-safe binary literal such that:
template <uint64_t V>
constexpr uint64_t make_binary() {
return V;
}
template <uint64_t V, char C, char... Cs>
constexpr uint64_t make_binary() {
static_assert(C == '0' || C == '1', "invalid binary");
return make_binary<2*V + C - '0', Cs...>();
}
template <char... Cs>
uint64_t operator "" _b()
{
static_assert(sizeof...(Cs) <= 64, "overflow");
return make_binary<0, Cs...>();
}
uint64_t a = 101_b; // OK: a == 5
uint64_t b = 102_b; // error: invalid
uint64_t c = 11111111110000000000111111111100000000001111111111000000000011111111110000000000_b; // error: overflow
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With