What is the difference between boost::path::string()
and boost::path::generic_string()
, and when should I use each of them?

- 378,754
- 76
- 643
- 1,055

- 17,104
- 45
- 172
- 321
-
2What did you discover when you read the documentation for each function? – Lightness Races in Orbit Oct 03 '14 at 12:15
-
I could not find documentation! – mans Oct 03 '14 at 12:16
-
5[Then you didn't look very hard...](https://www.google.co.uk/webhp?q=boost+path+string+documentation) – Lightness Races in Orbit Oct 03 '14 at 12:18
-
1@mans Seriously. It's less than 3 hours ago that I said the exact same thing... ["I've linked to the documentation page for cmd.exe options. \[...\] if you don't feel like reading the documentation yet."](http://stackoverflow.com/questions/26176172/how-can-i-find-why-system-can-not-run-my-application/26176412#comment41041133_26176412). You can't be a programmer when you ask others to do all the reading. – sehe Oct 03 '14 at 12:33
-
@Lightness It is unclear what an enthusiast or professional programmer could be asking when they ask this question. Clearly any enthusiast or professional programmer with internet access could google the docs: so an answer blatantly provided by said docs cannot be what an enthusiast or professional programmer wants to know. Eliminating that, it is very unclear what mans is asking. :p (If the docs where the C++ standard, next to nobody can read that and make sense of it (not even the writers sometimes): but the linked docs are not that obtuse) – Yakk - Adam Nevraumont Oct 03 '14 at 13:41
-
@Yakk Please see my edit in question! – mans Oct 03 '14 at 14:02
-
@sehe My question was how to get information from system and you kindly helped me on the subject. you pointed me to that I can use system to use cmd and run my command inside cmd and I could manage to fix my problem. I read the documentation for system and there was no information on the trick that you stated. – mans Oct 03 '14 at 14:05
-
@LightnessRacesinOrbit See, the problem was almost completely unrelated to what mans was apparently asking. ;) – Yakk - Adam Nevraumont Oct 03 '14 at 14:18
-
@Yakk: That's because he asked the wrong question, and now the question is totally different from what it used to be. We can and should only consider the question that is actually asked on screen, not the question hidden away in his head. Mans, please roll back and raise a new question for this new query. – Lightness Races in Orbit Oct 03 '14 at 15:06
2 Answers
This is clearly stated in the documentation; you need only read the documentation to gain knowledge and understanding. Please get into the habit of doing that, starting from now.
boost::path::string
Returns a std::string
in the native pathname format.
boost::path::generic_string
Returns a std::string
in the generic pathname format.
When to use each of them
Well, that's up to you, and depends on your needs! The following quotation, again from the documentation, may help…
[Note: For ISO/IEC 9945, no conversion occurs, since the native format and generic format are the same. For Windows, backslashes are converted to slashes --end note]
In day-to-day use, you can effectively say:
- On Windows, native format has backslashes and generic format has slashes;
- On Linux, both formats have slashes.

- 378,754
- 76
- 643
- 1,055
-
Thanks. Does any of them return path with "" so if the path has space, it can be used directly for passing to other commands? – mans Oct 03 '14 at 12:23
-
Many will stringify like that, IIRC (`outstream << path` adds them depending on platform and possibly content). This is non-authoritative, check the docs – sehe Oct 03 '14 at 12:28
-
12Stating that "this is clearly stated in the documentation" is a huge overstatement. It is everything but clear. Boost Filesystem documentation is an example of how not to write documentation. – Zbyl Dec 21 '16 at 14:26
-
@Zbyl: If you simply click on the link I provided in the first sentence of my answer, you'll be taken straight to a quite clear explanation of the difference. I'm not saying Boost.Filesystem has the best documentation in the world, but it does contain the answer to this question and the OP showed zero research effort (which was a serial habit of theirs, at the time). – Lightness Races in Orbit Dec 21 '16 at 14:32
-
Ah yes, clearly "path observers" is a really obvious term anyone would know. It's all very well it being 'clearly stated' in a different part of the documentation but as someone who came across this in legacy code I had exactly the same question and this question/answer was helpful. – Mr. Boy Jul 05 '21 at 20:55
Reading your mind, you are programming on a Windows system.
On your system, as far as boost can tell, the preferred separator between path elements is \
. However, /
is an acceptable separator.
The constructor to boost::fs::path
docs state:
[Note: For ISO/IEC 9945 and Windows implementations, the generic format is already acceptable as a native format, so no generic to native conversion is performed. --end note]
Note the clause about Windows implementations -- the generic format (with /
separators) is already acceptable, so no conversion is done.
Then when you invoke t/fn
the appends
or /
or /=
operator is used. It states:
[Note: For ISO/IEC 9945-like implementations, including Unix variants, Linux, and Mac OS X, the preferred directory separator is a single forward slash.
For Windows-like implementations, including Cygwin and MinGW, the preferred directory separator is a single backslash.--end note]
And the preferred separator is \
on windows systems.
So at construction, no conversion from generic to system occurs -- but on appending with operator/
or similar, it is.
This results in your string looking ugly.
If you want to fix the problem, you could iterate over your 'malformed' path using begin
and end
, and store/append the elements into a new path using 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 docs right will take your mixed-slash path and generate a clean one.
If you are stuck in C++03 iterate over in
using in.begin()
and in.end()
and boost::fs::path::iterator
.

- 262,606
- 27
- 330
- 524