6

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.

3 Answers 3

6

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()).

2
  • 1
    Thank you, that was really helpful. Can you suggest a way to make this code more portable? Should I declare my strings that refer to filesystem paths as boost::filesystem::path::string_type throughout my code? What's the good practice here?
    – Ramiro
    Aug 22, 2013 at 14:07
  • 2
    It depends what you want to do with the paths. The most portable, but most restrictive, thing is to stick to filesystem::path and use the file manipulation and iostream functionality provided by boost. If you need something more specific you lose the portability but gain flexibility. Aug 27, 2013 at 22:09
4

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();
1
  • 1
    This solves the compilation problem, but I guess my main concern now is to know if there are and what are the good practices to write a portable code for both Linux and Windows using Boost Filesystem path objects.
    – Ramiro
    Aug 23, 2013 at 12:51
4

Boost Path has a straightforward function set to give you a std::string in "native" (i.e. portable) format. Use make_preferred in combination with string. This is portable between the different operating systems supported by Boost, and also allows you to work in 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);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.