c++c++20user-defined-literals

Numeric UDL operator template


I'm trying to define what Pablo Halpern calls a numeric UDL operator template. I want it to return a lambda that would count how many characters at the beginning of a char array are from the given set.

Here's my test code:

template <char... c>
constexpr auto operator ""_cntany() {
  return [](const char* s){
    unsigned n = 0;
    while (((*s==c) || ...)) { ++n; ++s; }
    return n;
  };
}

int main(int argc, char** argv) {
  return ("test"_cntany)(argv[1]);
}

But this doesn't compile with GCC 11.2 (https://godbolt.org/z/TdbKzTMW8). Here's the error I'm getting:

<source>: In function 'int main(int, char**)':
<source>:11:15: error: no matching function for call to 'operator""_cntany<"test">()'
  11 |       return ("test"_cntany)(argv[1]);
      |               ^~~~~~~~~~~~~
<source>:2:20: note: candidate: 'template<char ...c> constexpr auto operator""_cntany()'
    2 |     constexpr auto operator ""_cntany() {
      |                    ^~~~~~~~
<source>:2:20: note:   template argument deduction/substitution failed:
<source>:11:15: error: '"test"' is not a valid template argument for type 'char' because string literals can never be used in this context
  11 |       return ("test"_cntany)(argv[1]);
     |               ^~~~~~~~~~~~~
Compiler returned: 1

What am I doing wrong?


Solution

  • cppreference says

    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 (in which case it is known as a numeric literal operator template)

    template <char...> double operator "" _x();
    

    Also see [over.literal.5]

    i.e. it's numeric-, and not a string literal.

    It can be used like 123_cntany.

    Note, there is a new C++20 string literal operator template: see this answer