6

I thought I'd found the right function: std::filesystem::path::concat to do something like

auto b = path(C:a/b); 
const auto c = b.concat(/c);

c has the right value, but b is mutated (it ultimately equals c).

I want b to remain unaffected. According to the documentation, concat is equivalent to operator+=. However, there's no operator+.

I think is a weird design, because there's the similar operator/= and operator/ where / means subdirectory instead of literal concat (like +=).

Can I do this non-mutating concat without something like const auto c = std::filesystem::path(b.string() + "/c")?

Tobi Akinyemi
  • 804
  • 1
  • 8
  • 24
  • 1
    As an aside, you could always handle this with a free function: `template std::filesystem::path concat_path(std::filesystem::path p, T && arg) { p += std::forward(arg); return p; }` – cdhowie Jul 28 '20 at 21:48
  • @FredLarson I spoke about the difference in the question, `/` appends as a subdirectory but `+= (concat)` is a literal path concat – Tobi Akinyemi Jul 28 '20 at 21:50
  • @TobiAkinyemi: Yes, I see that now. Sorry. – Fred Larson Jul 28 '20 at 21:50
  • @TobiAkinyemi The `p` argument is passed by-value (hence copied). So `auto a = concat_path(b, c);` is roughly the same as `auto a = b; a += c;`. (`b` is not mutated.) – cdhowie Jul 28 '20 at 21:54
  • @cdhowie I see, I thought about doing something like `temp = path.clone(); c = temp += "/c"` I guess your function wraps this up – Tobi Akinyemi Jul 28 '20 at 21:55
  • or `c = b; c += "/c"`. Is this just a matter of an operator that was forgotten to be added? – Tobi Akinyemi Jul 28 '20 at 22:00
  • 1
    @TobiAkinyemi See [this question](https://stackoverflow.com/questions/46137433/why-do-boostfilesystempath-and-stdfilesystempath-lack-operator) and [this LWG defect](https://timsong-cpp.github.io/lwg-issues/2668). Basically, `boost::filesystem::path` (on which the standard version is based) doesn't have an `operator+` and the committee doesn't want to add one because it would make the interface more complex. – Miles Budnek Jul 28 '20 at 23:41

0 Answers0