简体   繁体   中英

No operator+ for std::filesystem::path?

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?

This is a bit speculative, but I think the reason for this is that an operator+ could be easily confused with operator/ . This would then lead to bugs if used as follows

path p2{"/"};
auto p1 = p2 + "A" + "B";
// Wants /A/B, gets /AB

Using string literals makes the workaround nicer:

using namespace std::literals;
std::filesystem::path p1{"A"};
auto p2 = p1 / "B" / ("C"s + ".d");   

Here, "C"s creates a std::string with content C and then we use std::string 's operator+ . If the "C" part is itself already a path (otherwise you could just write "Cd" to begin with), you can do

std::filesystem::path p1{"A"}, c_path{"C"};
auto p2 = p1 / "B" / (c_path += ".d");   

since operator+= returns the resulting object. (This is a bit wasteful but I can imagine that the compiler will optimize that).

I though maybe it was because std::string and const char * implicitly convert to std::filesystem::path. Here is some c++20 being very careful to avoid unwanted implicit conversions.

#include <concepts>
#include <filesystem>
#include <iostream>
#include <string>
using namespace std::string_literals;
template <typename T, typename U>
requires std::same_as<std::decay_t<T>, std::filesystem::path> &&
    (!std::same_as<std::decay_t<U>, std::filesystem::path>) && 
        std::convertible_to<std::decay_t<U>, std::filesystem::path>
    inline std::filesystem::path
        operator+(const T &lhs, const U &rhs) {
    auto tmp = lhs;
    tmp += rhs;
    return tmp;
}

template <typename T>
requires std::same_as<std::decay_t<T>, std::filesystem::path> ||
    std::same_as<std::decay_t<T>, std::filesystem::directory_entry>
inline std::filesystem::path operator+(const std::filesystem::path &lhs,
                                       const T &rhs) {
    auto tmp = lhs;
    tmp += rhs;
    return tmp;
}
int main() {
    std::cout << "aaa" + std::filesystem::path{"bbb"} << '\n';
    std::cout << "aaa"s + std::filesystem::path{"bbb"} << '\n';
    std::cout << std::filesystem::directory_entry{"aaa"} +
                     std::filesystem::path{"bbb"}
              << '\n';

    std::cout << std::filesystem::path{"aaa"} + "bbb" << '\n';
    std::cout << std::filesystem::path{"aaa"} + "bbb"s << '\n';
    std::cout << std::filesystem::path{"aaa"} +
                     std::filesystem::directory_entry{"bbb"}
              << '\n';

    std::cout << std::filesystem::path{"aaa"} + std::filesystem::path{"bbb"}
              << '\n';
    std::cout << std::filesystem::directory_entry{"aaa"} +
                     std::filesystem::directory_entry{"bbb"}
              << '\n';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM