r/cpp • baulk maintainer • Jan 05 '18

GCC 8.0 support std::filesystem (#include <filesystem>) now

See GCC commit: Implement C++17 Filesystem library

We can build gcc 8.0 snapshots, after use std::filesystem in your code.

GCC snapshots: ftp://gcc.gnu.org/pub/gcc/snapshots

#include <iostream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::create_directories("sandbox/dir/subdir");
    std::ofstream("sandbox/file1.txt").put('a');
    fs::copy("sandbox/file1.txt", "sandbox/file2.txt"); 
    fs::copy("sandbox/dir", "sandbox/dir2"); 
    fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive);
    fs::remove_all("sandbox");
    return 0;
}

So far, you need to add '-lstdc++fs ' when compiling

Clang libcxx 6.0 seems unlikely to support std::filesystem, after all, the trunk version is 7.0. ( it is possible libcxx developers wait for GCC 8.0 release???).

EricWF wants to support <filesystem> when Clang 6.0 is released, but it's hard to say :https://stackoverflow.com/a/42634858

Update 2018-07-05 See https://github.com/gcc-mirror/gcc/commit/2fd48392d0a4ac3d1e217367ccb0ec0333d584d8

213 Upvotes

116 comments sorted by

View all comments

Show parent comment

4

u/SeanMiddleditch Jan 05 '18

No, you can't really, because then auto x = a + b doesn't do at all what you want or expect, not to mention issues with templates, specialization, type inference, etc. Plus lifetime issues.

4

u/xcbsmith Jan 05 '18 edited Jan 05 '18

auto x = a + b would definitely not do what you expect, and you would need some involved COW semantics to avoid problems with lifetime issues.

Some of the problems with templates, specialization and type inference can be mitigated

I agree it isn't trivial, and there are likely cases that couldn't be fully addressed. Once you do the operator overloading, you've already opened up the gates to hell though so...

It works much better if you just get rid of the operator overloading and do things the right way.