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?