fs::path p1 = "/q/b";
fs::path p2 = "/";
std::cout << p1 / p2 << std::endl;
will output /.
It is totally unexpected from so many perspectives:
- Purely logical: appending (that is how the operation is called, right?) something to something cannot overwrite the former.
- Current Unix path behaviour: for Linux "/a/b" is the same as "/a/b/", "/a/b//", ..., so anyone who works with it would expect appending behaviour, as opposed to overwriting.
- Safety: Dangerous things (erasing an existing value of
fs::path) should be much harder to do than safe/non-destructive things (just adding to an existing value offs::path). In other words, the current behaviour relies only on somebody's manually checking what is going on. If you forgot to write a check (whether a path starts from/) - you are out of luck. - Finally: I do not think a person who just wants to overwrite a path, will use
/instead of=.
My primary question, however, is what the supposed solution is? Please, imagine p2 path comes to you as an argument of a function, it might be / or c/d or c.
Some (awful) options I see:
- I can just remove the first if in sources of libcxx:
path& operator/=(const path& __p) {
if (__p.is_absolute()) {
__pn_ = __p.__pn_;
return *this;
}
Continue using
operator/- but if I want more compliant with Linux behaviour I need to have some checks?Use
concat, operator+=- but it will not add separator/in case offs::path("a") + "b".
Ideally I would like to overwrite the operator/...
Any suggestions?