相关疑难解决方法(0)

是否可以为字符串文字创建模板化的用户定义文字(文字后缀)?

当我发现可以将用户定义的文字模板化时,我感到很惊讶:

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:注意:模板参数推导/替换失败:

(爱迪生)

有没有办法将模板化的用户定义文字与字符串文字一起使用?

c++ templates c++11

5
推荐指数
1
解决办法
594
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1