If you have a path let us say /tmp/test/t
then I would expect the parent path to /tmp/test
.
Then if we have another path /tmp/test/t/
then I would still expect the parent path to be /tmp/test
.
However, the following code shows that this is wrong:
<code>#include <filesystem>
#include <iostream>
#include <string>
#include <cassert>
int main(void) {
auto parent_one = std::filesystem::path("/tmp/test/t/").parent_path();
auto parent_two = std::filesystem::path("/tmp/test/t").parent_path();
std::cout << parent_one << "n";
std::cout << parent_two << "n";
assert(parent_one == parent_two);
}
</code>
<code>#include <filesystem>
#include <iostream>
#include <string>
#include <cassert>
int main(void) {
auto parent_one = std::filesystem::path("/tmp/test/t/").parent_path();
auto parent_two = std::filesystem::path("/tmp/test/t").parent_path();
std::cout << parent_one << "n";
std::cout << parent_two << "n";
assert(parent_one == parent_two);
}
</code>
#include <filesystem>
#include <iostream>
#include <string>
#include <cassert>
int main(void) {
auto parent_one = std::filesystem::path("/tmp/test/t/").parent_path();
auto parent_two = std::filesystem::path("/tmp/test/t").parent_path();
std::cout << parent_one << "n";
std::cout << parent_two << "n";
assert(parent_one == parent_two);
}
Here parent_one
is /tmp/test/t
and parent_two
is /tmp/test
, where the later is what I would expect for both calls.
Is this correct behavior for std::filesystem::path
?
The code is compiled with g++ -std=c++17 main.cpp -o main
with g++ version
<code>g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</code>
<code>g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</code>
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.