问题描述
#include<iostream>
#include<fstream>
using namespace std::literals;
int main()
{
auto a = "st"sv;
std::ifstream in("test.txt"sv,std::ios::in); //error C2664
std::ifstream in("test.txt"s,std::ios::in);
}
我正在使用视觉工作室。我不能在 fstream 上使用 string-view literal ""sv
吗?还是我必须设置一些东西?
解决方法
没有。你不能。
您不能使用它,因为 std::ifstream
没有接受 std::string_view
ref 的构造函数
并且 std::string_view
和 if_stream
接受的类型之间没有隐式转换,因此您必须使用 staic_cast
或 std::string
如果你有一个std::string_view
(左值),你可以按如下方式使用
#include<iostream>
#include<fstream>
using namespace std::literals;
int main()
{
auto a = "st"sv;
auto file_location = "test.txt"sv;
std::ifstream in(std::string(file_location),std::ios::in);
}