It is possible to append multiple paths in a row using the /
operator:
std::filesystem::path p1{"A"};
auto p2 = p1 / "B" / "C";
which is rather convenient. However, concat only offers +=
:
std::filesystem::path p1{"A"};
auto p2 = p1 / "B" / "C" + ".d"; // NOT OK
This is pretty annoying, as I can't easily add extensions to the end of my paths. I have no choice but to write something like
std::filesystem::path p1{"A"};
auto p2 = p1 / "B" / "C";
p2 += ".d";
Am I missing something? Is there a reason for this inconsistency?
const char[N]
, that would beauto p2 = p1 / "B" / (std::string("C") + ".d");
– Kelso("C" + ".d")
adds twoconst char[2]
s – Uneasy"A"+"B"
– Kelso"C"
will most likely also be astd::filesystem::path
, so the second option won't work. The first one is bizarre, but should work. :) – Pahlauto p2 = p1 / "B" / "C" += ".d";
orauto p2 = p1 / "B" / (std::string() + "C" + ".d");
:D . Both of the these work in godbolt.note that in practice, "C" will most likely
Not in practice, in any case the result ofp1 / "B" / "C"
has typestd::filesystem::path
, so it's doing+=
on it. Still, this is not an answer, as I do not know "the reason for this inconsistency". – Hannahannah+=
to rvalue there's #65462389 . Apath()
call will help ((path(a) += b)
) if the object inside is a lvalue. – Nakada