当我发现可以将用户定义的文字模板化时,我感到很惊讶:
template <char ...C> std::string operator ""_s()
{
char arr[]{C...};
return arr;
}
// ...
std::cout << 123_s;
Run Code Online (Sandbox Code Playgroud)
但以上声明不适用于字符串文字:
"123"_s
Run Code Online (Sandbox Code Playgroud)
给我以下错误:
prog.cpp:在函数'int main()'中:
prog.cpp:12:15:错误:没有匹配的函数可调用'operator“” _ s()'
std :: cout <<“ 123” _s;prog.cpp:4:34:注意:候选:模板std :: string运算
符“” _s()模板std :: string运算符“” _s()prog.cpp:4:34:注意:模板参数推导/替换失败:
有没有办法将模板化的用户定义文字与字符串文字一起使用?