为什么boost :: filesystem :: path和std :: filesystem :: path缺少operator +?

Quu*_*one 13 c++ boost-filesystem c++17

考虑以下关于路径分解的断言,其中每个局部变量例如stem具有明显的初始化,例如auto stem = path.stem()-

assert(root_path == root_name / root_directory);
assert(path == root_name / root_directory / relative_path);
assert(path == root_path / relative_path);

assert(path == parent_path / filename);
assert(filename == stem + extension);
Run Code Online (Sandbox Code Playgroud)

这一切都有效,除了最后一行 - 因为fs::path没有定义operator+.它有operator+=,但没有operator+.

这是什么故事?


我已经确定我可以通过添加自己的代码来编译代码operator+.有什么理由不这样做吗?(请注意,这是在我自己的命名空间中;我没有重新打开namespace std.)

fs::path operator+(fs::path a, const fs::path& b)
{
    a += b;
    return a;
}
Run Code Online (Sandbox Code Playgroud)

我对这个问题的唯一假设是:

  • 也许设计师担心operator+会太容易混淆使用std::stringoperator+.但这看起来很愚蠢,因为它在语义上完全相同(所以为什么要关心它是否会混淆?).而这也似乎设计师不关心新手的困惑,当他们设计path.append("x")做一些语义不同str.append("x")path.concat("x")做一些语义相同str.append("x").

  • 也许path隐含的转换operator string_type() const会导致某些变化p + q模糊不清.但我一直无法提出任何此类案件.

CHK*_*ley 4

这是作为文件系统库的缺陷输入的,由于接口的复杂性以及通过转换为字符串和返回路径来解决问题的能力,它被判断为不是缺陷。在这里阅读所有相关内容:https://timsong-cpp.github.io/lwg-issues/2668

  • @TomAranda 听起来像是我的答案。 (6认同)
  • 哇,这真的很烦人。filesystem::path 不是旨在使路径连接简单且可读吗?简单的情况outputDirPath / fileBasePath +“.extension”需要转换。这么难看。谁会关心在一个巨大的库中定义的另外 12 个运算符呢?我使用该库来使我的代码更简单。否则我可以首先连接字符串。 (4认同)