boost :: filesystem :: path :: native()返回std :: basic_string <wchar_t>而不是std :: basic_string <char>

Ram*_*iro 6 c++ boost

虽然以下代码在Linux上编译,但我无法在Windows上编译它:

boost::filesystem::path defaultSaveFilePath( base_directory );
defaultSaveFilePath = defaultSaveFilePath / "defaultfile.name";
const std::string s = defaultSaveFilePath.native();
return save(s);
Run Code Online (Sandbox Code Playgroud)

其中base_directory是类的属性,其类型是std :: string,而函数save只需要一个const std :: string&作为参数.编译器抱怨第三行代码:

错误:从'const string_type {aka const std :: basic_string}'转换为非标量类型'const string {aka const std :: basic_string}'required"

对于这个软件,我使用的是Boost 1.54(对于一些常见的库)和Qt 4.8.4(对于使用这个公共库的UI),我使用MingW GCC 4.6.2编译了所有内容.

似乎我的Windows Boost构建由于某种原因返回std :: basic_string.如果我的评估是正确的,我问你:我如何使Boost返回std :: string的实例?顺便说一下,有可能吗?

如果我对问题做了不好的评价,请您提供一些有关如何解决问题的见解.

干杯.

Ala*_*kes 6

在Windows上,boost :: filesystem表示wchar_t设计中的本机路径- 请参阅文档.这非常有意义,因为Windows上的路径可以包含非ASCII Unicode字符.你无法改变这种行为.

请注意,std::string只是std::basic_string<char>,并且所有本机Windows文件函数都可以接受宽字符路径名(只调用FooW()而不是Foo()).

  • 这取决于你想要对路径做什么.最便携但最具限制性的是坚持使用`filesystem :: path`并使用boost提供的文件操作和iostream功能.如果您需要更具体的东西,则会失去便携性但会获得灵活性. (2认同)