相关疑难解决方法(0)

std :: filesystem :: path和std :: string之间的隐式转换,应该发生吗?

状态的cppreference页面std::filesystem::path

路径可以隐式转换为from和from std::basic_strings,因此可以将它们与over files API一起使用,例如,作为std::ifstream::open

现在,转换为a std::filesystem::path很容易,因为它具有采用std::string类型的非显式构造函数。但是,我似乎找不到的是一种std::string隐式地进行查找的方法。

有一个string功能,但是std::string string() const;没有operator std::string()。使用

#include <filesystem>

void foo(std::string) {}

int main()
{
    namespace fs = std::filesystem;
    fs::path p1;
    foo(p1);
}
Run Code Online (Sandbox Code Playgroud)

这段代码可以使用iccgccclang很好地编译,但是不能使用MSVS编译,但会给出错误:

example.cpp

<source>(10): error C2664: 'void foo(std::string)': cannot convert argument 1 from 'std::filesystem::path' to 'std::string'

<source>(10): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot …
Run Code Online (Sandbox Code Playgroud)

c++ path language-lawyer implicit-conversion std-filesystem

2
推荐指数
2
解决办法
120
查看次数