为什么std :: filesystem :: path :: compare()和boost :: filesystem :: path :: compare()不同?

问题描述 投票:1回答:1

给出以下代码:

  fs::path p{ "a/b/" };
  fs::path q{ "a/b/." };  
  assert(p == q);         

[[注意定义q的字符串末尾的多余点。]

在上面,boost :: filesystem接受p == q为true,但std :: filesystem表示它为false。

还请注意,std和boost都同意这是正确的:

  auto pln = p.lexically_normal();
  auto qln = q.lexically_normal();
  assert(pln == qln);

在我看来,这很重要,因为路径比较不是std :: string比较。例如,boost和std :: filesystem都同意这是正确的:

  fs::path x{ "a/.././b" };
  fs::path y{ "a///..//.////b" };
  assert(x == y);

我的理解方式是,在对路径进行归一化之后,应该在概念上进行比较。 (实际上,有一些优化。)

所以,为什么boost和std之间有差异?

P.S。这是标准的内容:

29.11.7.4.8比较[fs.path.compare]int compare(const path&p)const noexcept;返回值:—令rootNameComparison为this-> root_name()。native()。compare(p.root_- name()。native())的结果。如果rootNameComparison不为0,则为rootNameComparison。—否则,如果!this-> has_root_directory()和p.has_root_directory(),则该值小于0。—否则,如果this-> has_root_directory()和!p.has_root_directory(),则该值大于0。—否则,如果按字典顺序对this-> relative_path()的元素的native()对于p.relative_path()的元素,其值小于native(),其值小于0。—否则,如果按字典顺序对this-> relative_path()的元素的native()对于p.relative_path()的元素,其值大于native(),其值大于0。—否则,0。
c++ boost-filesystem std-filesystem
1个回答
0
投票

简短的答案是,Unix上的.是实际的文件系统节点链接,有时很重要。参见this stack overflow answer

© www.soinside.com 2019 - 2024. All rights reserved.