Question

So std::map wants the key type to be ordered, but I ran into problems:

"a/b" < "a/c" < "a//b" but fs::equivalent("a/b", "a//b")

"a/b" < "a/c" < "a\b" but fs::equivalent("a/b", "a\b")

How I do I normalize a path enough to create an ordering? Is that even possible? I tried using path::generic_string() in a custom comparison operator, but it didn't work. fs::equivalent() doesn't help either, because implementing equality isn't enough for std::map, it needs an ordering. That's also the reason why this question isn't a duplicate of How do I "normalize" a pathname using boost::filesystem?.

Context: Windows only, Boost 1.49, filesystem3.

Was it helpful?

Solution

To make paths comparable you need to convert them to the canonical representation and then compare via string. A canonical path is absolute, normalized and has symbolic links removed. Boost offers canonical AFAIK. Because symbolic links need to be resolved, calling canonical requires access to the filesystem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top