template parameter list for literal operator
E

3

8

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...>();
    }
}

GCC compiles happily,

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;
                      ^

icc also fails:

error: a literal operator template must have a template parameter list equivalent to "<char ...>"

    constexpr int operator"" _b()

                  ^

MSVC also fails:

<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.

Effusive answered 15/12, 2018 at 2:26 Comment(0)
K
7

The standard spells it out pretty explicitly in [over.literal]/5:

The declaration of a literal operator template shall have an empty parameter-declaration-clause and its template-parameter-list shall have a single template-parameter that is a non-type template parameter pack with element type char.

So GCC is wrong in allowing this.

Knowle answered 15/12, 2018 at 2:37 Comment(0)
P
3

I don't know who's right but I propose a C++17 alternative: comma operator and template folding instead of recursion

   template <char ... Chs>
   constexpr int operator"" _b()
    {
      int ret {};

      return ((ret <<= 1, ret += Chs - '0'), ...);
   }
Pump answered 15/12, 2018 at 2:36 Comment(1)
@陳力 - You're welcome. I find very funny template metaprogramming.Pump
S
2

Literal operator template of the form

template <class T, T... >
constexpr int operator "" _b();

is a clang and gcc extension, MSVC don't support that extension.

There was however a proposal to reconsider literal operator templates

Squeegee answered 15/12, 2018 at 2:40 Comment(1)
P0424 was abandoned for something much better: P0732. It just gives you the ability to make array objects that act like literal strings (among many other things). And it's already in the working paper for C++20.Knowle

© 2022 - 2024 — McMap. All rights reserved.