为什么BOOST :: fileSystem :: path and std :: filesystem ::路径含量含

Why do boost::filesystem::path and std::filesystem::path lack operator+?

本文关键字:filesystem 路径 std and BOOST fileSystem path 为什么      更新时间:2023-10-16

考虑有关路径分解的以下断言,其中每个局部变量例如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);

这一切都起作用,除了最后一行 - 因为fs::path没有定义operator+。它具有operator+=,但没有operator+

这里的故事是什么?


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

fs::path operator+(fs::path a, const fs::path& b)
{
    a += b;
    return a;
}

我对该主题的唯一假设是:

  • 也许设计师担心operator+std::stringoperator+相混淆。但这似乎很愚蠢,因为它在语义上完全做得完全相同(那么为什么要关心它是否混淆?)。而且似乎设计师在设计path.append("x")时不在乎Newbies的混乱,从而在语义上与str.append("x")path.concat("x")进行语义不同的来做一些与str.append("x")相同的相同的事情。

  • 也许path的隐式转换operator string_type() const会导致某些p + q变得模棱两可。但是我一直无法提出任何这样的情况。

这是针对文件系统库的缺陷,并且由于接口的复杂性以及通过转换为字符串和返回路径而解决的能力,因此被认为不是一个缺点。在此处阅读所有内容:https://timsong-cpp.github.io/lwg-issues/2668