1

I tried the compile-time regex from this link and applied some of its utilization:

#include <string_view>
#include <iostream>
#include <https://raw.githubusercontent.com/hanickadot/compile-time-regular-expressions/master/single-header/ctre.hpp>

void match(std::string_view sv) noexcept {
    std::cout << ctre::match<"1.*">(sv) << '\n';
}

int main() {
    std::cout << std::boolalpha;

    match("1234");
    match("211");  
    match("1111"); 
}
// OUTPUT:
/**
true
false
true
*/

I used -std=c++20 flag, and it works perfectly fine.

I tried this on my own to see if non-type template parameters could work:

template <auto input> 
void print_ct() {
    std::cout << input << '\n';
}

int main() {
    print_ct<'A'>();
    print_ct<3>();
    print_ct<std::numbers::pi_v<float>>();  // Allowed since C++20
}
// OUTPUT:
/**
A
3
3.14159
*/

It also works fine, but when I use something like strings, it won't compile:

print_ct<"Hello">();

Why can't I use string literals as a non-type template parameter when ctre::match<"1.*">(sv) compiles perfectly?

Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
  • This is indeed specifically disallowed in the standard: [\[temp.arg.nontype\]/3.2](https://timsong-cpp.github.io/cppwp/n4861/temp.arg.nontype#3.2). There was a paper that proposed allowing string literals as template arguments: [P0424 String literals as non-type template parameters](https://wg21.link/p0424) – L. F. May 30 '21 at 04:03

0 Answers0