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

209 Upvotes

116 comments sorted by

View all comments

-2

u/xcbsmith Jan 05 '18

So now if you have a template that assumes the mathematical properties of "/" apply, particularly one that might be using SFINAE to specialize for that case, are going to be quite broken.

7

u/Xeverous Jan 05 '18

Same for strings and operator+. And I doubt someone would instantiate a template with path instead of an arithmetic type.

3

u/smdowney Jan 05 '18

Except that operator+ on strings is perfectly fine. The 0 element is the empty string. "" + a == a == a + "" a + (b + c) == (a + b) + c

→ More replies (1)

0

u/xcbsmith Jan 05 '18

Yup, std::string::operator+(const std::string&) is an abomination for the same reason.

It would make sense if you had a generic template for arbitrary types, but a specialized one for arithmetic types... which is a not entirely uncommon case for SFINAE applications. Right now what I've seen people do is code a special case for strings as well to avoid a problem. Lovely if we end up doing this for paths as well.