Why can't I directly pass string literals as non-type template parameter?

52 Views Asked by At

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?

0

There are 0 best solutions below