What is the difference between path :: string () and path :: generic_string () in boost?
This is clearly stated in the documentation ; you only need to read the documentation to gain knowledge and understanding. Please get used to this from now on.
nudge :: path :: string
Returns a std::string
in native pathname format .
nudge :: path :: generic_string
Returns a std::string
in general path format .
When to use each one
Well, that's up to you, and depends on your needs! The following quote, again from the documentation, might help & hellip;
[Note. For ISO / IEC 9945, no conversion occurs because the native format and the common format are the same. For Windows, backslash is converted to slides - note]
In everyday use, you can effectively say:
- On Windows, native format is backslash and general format is slash.
- On Linux, both formats have a slash.
source to share
By reading your mind, you are programming on the Windows system.
On your system, as far as can be said, the preferred separator between path elements \
. However, it /
is an acceptable delimiter.
Constructor boost::fs::path
docs:
[Note. For ISO / IEC 9945 and Windows implementations, the generic format is already acceptable as a native format, so no generic native conversion is performed. - note]
Note the Windows implementation proposal - the generic (delimited /
) format is already valid, so no conversion is done.
Then when you call t/fn
, the operator is used appends
, or /
, or /=
. It states:
[Note: For implementations of ISO / IEC 9945, including Unix flavors, Linux, and Mac OS X, the single forward slash is the preferred directory separator.
For Windows-like implementations, including Cygwin and MinGW, the preferred directory separator is a single backslash. --End note]
And the preferred separator \
for Windows systems.
Thus, when building, there is no transition from the general to the system, but when added using operator/
or a similar value, it is.
As a result, your string looks ugly.
If you want to fix this problem, you can iterate over your "wrong" path with begin
and end
and save / add items to the new path with operator/
.
boost::fs::path fix( boost::fs::path in ) {
boost::fs::path retval;
for ( auto element : in ) {
if (retval.empty())
retval = in;
else
retval /= in;
}
return retval;
}
which, if I read the right of the docs, will take your mixed slash path and create a clean file.
If you are stuck with C ++ 03 iterating over in
with in.begin()
and in.end()
and boost::fs::path::iterator
.
source to share