#include <iostream>
template <size_t n, char s[n]>
class X {
public:
X() {
std::cout << s;
std::cout << std::endl;
}
};
char hey[] = "hey";
int main() {
X<4, hey> x;
}
But X<4, "hey"> x;
does not compile because, for a non-type template argument, certain restrictions apply:
For pointers to objects, the template arguments have to designate the
address of an object with static storage duration and a linkage
(either internal or external), or a constant expression that evaluates
to the appropriate null pointer or std::nullptr_t value.
This raises another issue, I found the following on cppreference.com:
Array and function types may be written in a template declaration, but
they are automatically replaced by pointer to object and pointer to
function as appropriate.
So s
is actually a pointer, therefore the following will compile:
X<5, hey> something;
Potential buffer-overflow problem.