[英]boost::filesystem::path::native() returns std::basic_string<wchar_t> instead of std::basic_string<char>
Although the following code compiles on Linux, I'm no being able to compile it on Windows: 虽然以下代码在Linux上编译,但我无法在Windows上编译它:
boost::filesystem::path defaultSaveFilePath( base_directory );
defaultSaveFilePath = defaultSaveFilePath / "defaultfile.name";
const std::string s = defaultSaveFilePath.native();
return save(s);
where base_directory is an attribute of a class and its type is std::string, and function save simply takes a const std::string & as argument. 其中base_directory是类的属性,其类型是std :: string,而函数save只需要一个const std :: string&作为参数。 The compiler complains about the third line of code:
编译器抱怨第三行代码:
error: conversion from 'const string_type {aka const std::basic_string}' to non-scalar type 'const string {aka const std::basic_string}' requested" 错误:从'const string_type {aka const std :: basic_string}'转换为非标量类型'const string {aka const std :: basic_string}'required“
For this software, I'm using both Boost 1.54 (for some common libraries) and Qt 4.8.4 (for the UI that uses this common library) and I compiled everything with MingW GCC 4.6.2. 对于这个软件,我使用的是Boost 1.54(对于一些常用库)和Qt 4.8.4(对于使用这个公共库的UI),我使用MingW GCC 4.6.2编译了所有内容。
It seems that my Windows Boost build returns std::basic_string for some reason. 似乎我的Windows Boost构建由于某种原因返回std :: basic_string。 If my assesment is correct, I ask you: how do I make Boost return instances of std::string?
如果我的评估是正确的,我问你: 我如何使Boost返回std :: string的实例? BTW, is it possible?
顺便说一下,有可能吗?
If I made a bad evaluation of the problem, I ask you to please provide some insight on how to solve it. 如果我对问题做了不好的评价,请您提供一些有关如何解决问题的见解。
Cheers. 干杯。
On Windows, boost::filesystem represents native paths as wchar_t
by design - see the documentation . 在Windows上,boost :: filesystem根据设计将本机路径表示为
wchar_t
- 请参阅文档 。 That makes perfect sense, since paths on Windows can contain non-ASCII Unicode characters. 这非常有意义,因为Windows上的路径可以包含非ASCII Unicode字符。 You can't change that behaviour.
你无法改变这种行为。
Note that std::string
is just std::basic_string<char>
, and that all native Windows file functions can accept wide character path names (just call FooW() rather than Foo()). 请注意,
std::string
只是std::basic_string<char>
,并且所有本机Windows文件函数都可以接受宽字符路径名(只调用FooW()而不是Foo())。
how do I make Boost return instances of std::string?
如何使Boost返回std :: string的实例? BTW, is it possible?
顺便说一下,有可能吗?
How about string()
and wstring()
functions? string()
和wstring()
函数怎么样?
const std::string s = defaultSaveFilePath.string();
there is also 还有
const std::wstring s = defaultSaveFilePath.wstring();
Boost Path has a straightforward function set to give you a std::string in "native" (ie portable) format. Boost Path有一个简单的函数集,可以为您提供“本机”(即便携式)格式的std :: string。 Use
make_preferred
in combination with string
. 将
make_preferred
与string
结合使用。 This is portable between the different operating systems supported by Boost, and also allows you to work in std::string
. 这在Boost支持的不同操作系统之间是可移植的,并且还允许您在
std::string
。
It looks like this: 它看起来像这样:
std::string str = (boost::filesystem::path("C:/Tools") / "svn" / "svn.exe").make_preferred().string();
Or, modifying the code from the original question: 或者,修改原始问题的代码:
boost::filesystem::path defaultSaveFilePath( base_directory );
defaultSaveFilePath = defaultSaveFilePath / "defaultfile.name";
auto p = defaultSaveFilePath.make_preferred(); // convert the path to "preferred" ("native") format.
const std::string s = p.string(); // return the path as an "std::string"
return save(s);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.