Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

boost

Although the following code compiles on Linux, I'm no being able to 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 a class and its type is std::string, and function save simply takes a const std::string & as argument. 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"

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.

It seems that my Windows Boost build returns std::basic_string for some reason. If my assesment is correct, I ask you: how do I make Boost return instances of 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.

like image 397
Ramiro Avatar asked Aug 21 '13 19:08

Ramiro


2 Answers

On Windows, boost::filesystem represents native paths as wchar_t by design - see the documentation. That makes perfect sense, since paths on Windows can contain non-ASCII Unicode characters. 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()).

like image 145
Alan Stokes Avatar answered Sep 29 '22 08:09

Alan Stokes


how do I make Boost return instances of std::string? BTW, is it possible?

How about string() and wstring() functions?

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

there is also

const std::wstring s = defaultSaveFilePath.wstring();
like image 20
Afriza N. Arief Avatar answered Sep 29 '22 07:09

Afriza N. Arief