简体   繁体   English

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

[英]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_preferredstring结合使用。 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.

相关问题 转换std :: basic_string <wchar_t> 和std :: basic_string <uint16_t> - Convert std::basic_string<wchar_t> and std::basic_string<uint16_t> 将const wchar_t *转换为const std :: basic_string的更好方法<char_t> - Better way to convert const wchar_t * to const std::basic_string<char_t> 没有合适的构造函数来将“ const char [8]”转换为“ std :: basic_string” <wchar_t, std::char_traits<wchar_t> ,std :: allocator <wchar_t> &gt;” - no suitable constructor exists to convert from “const char [8]” to “std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>” 无法从“ TCHAR [260]”转换为“ std :: basic_string” <wchar_t,std::char_traits<wchar_t> ,性病::分配器 <wchar_t> &gt; - Cannot convert from 'TCHAR [260]' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 从&#39;std :: wstring {aka std :: basic_string <wchar_t> }&#39;转换为非标量类型&#39;UString {aka std :: basic_string <char> }&#39; - conversion from ‘std::wstring {aka std::basic_string<wchar_t>}’ to non-scalar type ‘UString {aka std::basic_string<char>}’ gdb PrettyPrinter插件例程StdStringPrinter在处理std :: basic_string时崩溃 <wchar_t(,.*)?> $ - gdb PrettyPrinter Plugin routine StdStringPrinter crashing when dealing with std::basic_string<wchar_t(,.*)?>$ boost :: interprocess :: basic_string as std :: string - boost::interprocess::basic_string as std::string 转换 std::basic_string<Char> 串起来 - Convert std::basic_string<Char> to string 如何将 wchar_t[] 转换为 basic_string<_Elem>? - How to convert wchar_t[] to basic_string<_Elem>? std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment|
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM