【发布时间】:2021-12-10 22:07:00
【问题描述】:
从std::string_view 转换为std::string 的这段代码怎么可能编译:
struct S {
std::string str;
S(std::string_view str_view) : str{ str_view } { }
};
但是这个不编译?
void foo(std::string) { }
int main() {
std::string_view str_view{ "text" };
foo(str_view);
}
第二个给出错误:cannot convert argument 1 from std::string_view to std::string 和 no sutiable user-defined conversion from std::string_view to std::string exists。
我应该如何正确调用foo()?
【问题讨论】:
-
您从第二个示例中得到的错误是什么?请将它们完整完整(并作为文本)复制粘贴到您的问题中。
标签: c++ string c++17 implicit-conversion string-view