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 comments

24

u/flashmozzg Jan 05 '18

Do you have the same problems with + used for string concatenation?

14

u/SeanMiddleditch Jan 05 '18

Oh, that's one of the worst, though in part because C++ - like most other languages(*) - can't collapse operators together into a single optimized function. That is, C++ turns a + b + c into operator+(operator+(a, b), c) instead of something like operator+(a, b, c).

It is thus pretty much always preferable where efficiency matters (and if we're using C++, odds are it's because we care about efficiency, I'd wager) to write and use a join(string...)-type function that can measure the lengths of all inputs, allocate a single result string, and then copy contents of each of the inputs exactly once.

It's arguably in C++ even better to use when possible a function of the form join(string& output, string_view...) so that you don't even have to allocate a new string if you have one that's already got space allocated. I just a couple weeks ago had to do just that in some tools code that enumerated a bunch of synthetic file paths out of a particular highly-optimized archive file; just forming the paths was taking about 95% of the execution time because of all the allocations in forming a string out of component pieces. By just restructuring to format the paths into an existing std::string and reusing allocated space cut execution time from multiple minutes to barely a dozen seconds!

(*) There are a handful of languages with built-in string types where the compiler/runtime can collapse string concatenations to fuse allocations and avoid excessive copies, but C++ isn't one of those. At one point in V8 (Chrome's JavaScript engine) it was faster to repeatedly concatenate strings than to use a buffer, as the runtime internally optimized the concatenations better than it could optimize a hand-rolled buffering code. High level language performance for the win. :p

9

u/xcbsmith Jan 05 '18

In fairness, you can do a clever implementation of operator+ that returns back a transient object that represents the composable operation.

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.

→ More replies (0)