【发布时间】:2019-12-14 02:01:28
【问题描述】:
std::filesystem::path 的 cppreference 页面指出:
路径可以隐式地与
std::basic_strings相互转换,这使得它们可以与文件API 一起使用,例如作为std::ifstream::open的参数
现在很容易看到转换为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);
}
此代码与icc、gcc 和clang 一起编译得很好,但与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 be called
Compiler returned: 2
那么,哪个编译器是正确的?是否有隐式转换序列,或者编译器只是有帮助?
【问题讨论】:
标签: c++ path language-lawyer implicit-conversion std-filesystem