Here is my implementation of converting binary literal to decimal:
template<char Head, char... Tail>
constexpr int operator"" _b()
{
if constexpr (sizeof... (Tail) == 0)
{
return Head - '0';
}
else
{
return (Head - '0') * (1 << sizeof...(Tail)) + operator"" _b<Tail...>();
}
}
while Clang fails:
prog.cc:1:2: error: template parameter list for literal operator must be either 'char...' or 'typename T, T...'
template<char Head, char... Tail>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:19:27: error: no matching literal operator for call to 'operator""_b' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template
std::cout << 110110110_b;
^
error: a literal operator template must have a template parameter list equivalent to "<char ...>"
constexpr int operator"" _b()
^
<source>(2): error C3686: 'operator ""_b': literal operator template must have exactly one template parameter that is a parameter pack
So, icc requires char...
while clang and msvc require typename T, T...
or char...
, only gcc allow my Head
and Tail
.
The workaround should be simple ---- just replace char Head, char... Tail
with char... digits
and return a new aux
function which using char Head, char... Tail
as template parameters, or use a struct then specialize head
and head, tail...
without if constexpr
.
But I didn't find related requirement from the standard draft. Can you tell me which one conforms the standard? Of course, if you have more elegant solution(besides the two I mentioned above) which will not invoke the compiler errors, please paste here, I'll very appreicate.