Boost :: filesystem :: path :: native () returns std :: basic_string <wchar_t> instead of std :: basic_string <char>

Although the following code compiles on Linux, I cannot compile it on 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 the class, and its type is std :: string, and the save function simply accepts const std :: string as an argument. The compiler complains about the third line of code:

error: conversion from 'const string_type {aka const std :: basic_string}' to the non-scalar type 'const string {aka const std :: basic_string}' requested "

For this software, I use both Boost 1.54 (for some common libraries) and Qt 4.8.4 (for the user interface that uses this shared library), and I compiled everything with MingW GCC 4.6.2.

It looks like my build of Windows Boost for some reason returns std :: basic_string. If my estimate is correct, I ask you: how do I make instances of Boost return std :: string? BTW, is this possible?

If I made a bad assessment of the problem, please give me some idea of ​​how to solve it.

Greetings.

+4
source share
3 answers

On Windows, boost :: filesystem presents its own paths as wchar_t for design - see the documentation. This makes sense because paths on Windows may contain Unicode characters other than ASCII. You cannot change this behavior.

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 (), not Foo ()).

+5
source

how do i make instances of boost return std :: string? BTW, is this possible?

What about the string() and wstring() functions?

 const std::string s = defaultSaveFilePath.string(); 

also exists

 const std::wstring s = defaultSaveFilePath.wstring(); 
+2
source

Boost Path has a simple set of functions that gives you std :: string in a "native" (i.e. portable) format. Use make_preferred in combination with string . This is portable between various operating systems supported by Boost, and also allows working in std::string .

It looks like this:

 std::string str = (boost::filesystem::path("C:/Tools") / "svn" / "svn.exe").make_preferred().string(); 

Or by changing 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); 
+2
source

Source: https://habr.com/ru/post/1498263/


All Articles