c++ - boost::filesystem::path::lexically_normal: 这是不正确的行为吗?

标签 c++ boost

boost::filesystem::path::lexically_normal() 的文档指出:

Returns *this with redundant current directory (dot), parent directory (dot-dot), and directory-separator elements removed.

参见:http://www.boost.org/doc/libs/1_63_0/libs/filesystem/doc/reference.html .

以下打印 ./test(使用 Boost 1.63.0),我希望 test:

#include <boost/filesystem/path.hpp>
#include <iostream>

int main(void)
{
   std::cout << boost::filesystem::path{"./test"}.lexically_normal().string() << "\n";
   return 0;
}

所以第一个点元素不被认为是多余的。但是,test./test 在使用 boost 文件系统时显然解析为同一个文件,所以这对我来说似乎不一致。这是预期的行为吗?

更新:C++17 文件系统库按预期返回“test”(GCC 8.2.0,Linux)。

最佳答案

这是合理的行为,因为尽管您认为 test./test 通常指的是同一事物,但并非所有地方都是这种情况。

例如,如果您在 shell 中将 ./test 作为命令运行,它将始终在当前目录中查找该程序,而不会在其他任何地方查找。但是,如果您运行 test,它将查找运行时路径(例如 $PATH)。

所以 test./test 是否引用同一个文件实际上是上下文相关的——因此 ./ 不是多余的。

关于c++ - boost::filesystem::path::lexically_normal: 这是不正确的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42952605/

相关文章:

c++ - C++ 中 vector 的 insert 和 emplace 有什么区别

c++ - 如何使用 boost property_tree 创建 xml

c++ - 通过键查找 Boost BGL 顶点

c++ - boost::asio 允许在连接处理程序阻塞时非阻塞地接受新连接

Boost 读/写 XML 文件 : how to change the characters encoding?

c++ - 为什么我总是必须除以 49 才能得到二进制转换器 + cout 歧义的正确答案?

c++ - 使用 "Uncopyable"类时静音 GCC 警告

c++ - 多线程上的信号量和临界区问题

c++ - Boost共享锁创建失败

c++ - 检测移除打开的串口设备(Qt/Windows)