Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::filesystem::path::lexically_normal: is this incorrect behavior?

Tags:

c++

boost

The documentation of boost::filesystem::path::lexically_normal() states:

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

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

The following prints ./test (using Boost 1.63.0) where I would expect test:

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

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

So the first dot element is not considered redundant. However, both test and ./test obviously resolve to the same file when using boost filesystem, so this seems inconsistent to me. Is this expected behavior?

Update: the C++17 filesystem library returns "test" as expected (GCC 8.2.0, Linux).

like image 386
Ton van den Heuvel Avatar asked Mar 22 '17 13:03

Ton van den Heuvel


1 Answers

It is reasonable behaviour, because although you are right that test and ./test usually refer to the same thing, this is not the case everywhere.

For example, if you run ./test as a command in a shell, it will always look for that program in the current directory and nowhere else. But if you run test it will look in the runtime path (e.g. $PATH) instead.

So whether test and ./test refer to the same file is actually context dependent--therefore the ./ is not redundant.

like image 56
John Zwinck Avatar answered Sep 21 '22 01:09

John Zwinck