@GeruzoniAnsasu c++2a 可以使用字符串模版
template<size_t N>
struct StringLiteral {
constexpr StringLiteral(const char (&str)[N]) {
std::copy_n(str, N, value);
}
char value[N];
};
template<StringLiteral lit>
void Print() {
// The size of the string is available as a constant expression.
constexpr auto size = sizeof(lit.value);
// and so is the string's content.
constexpr auto contents = lit.value;
std::cout << "Size: " << size << ", Contents: " << contents << std::endl;
}
int main(int argc, const char * argv[]) {
Print<"literal string">(); // Prints "Size: 15, Contents: literal string"
return 0;
}