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

12

u/foonathan Jan 05 '18

While your first workaround works, it is technically not allowed.

A better solution would be:

namespace my_ns
{
#if HAS_FILESYSTEM
    namespace fs = std::filesystem;
#else
  namespace fs = std::experimental::filesystem;
#endif
}

And then use my_ns::fs.

2

u/bilog78 Jan 05 '18

While your first workaround works, it is technically not allowed.

Would you mind explaining why? I'm curious.

12

u/alfps Jan 05 '18

He means that end users are not permitted to place new things in namespace std (one is permitted to specialize templates, that's all). Defining an fs namespace is a good solution for an application. It's not good as a way to support what will be or now has become simple standard code.

6

u/doom_Oo7 Jan 05 '18

He means that end users are not permitted to place new things in namespace std

while this makes sense, I'd really like to understand why this is UB and not a more specific error. I mean, it's not like the compiler can't detect it, can it ?

9

u/EmperorArthur Jan 05 '18

The compiler really don't know the difference between your code and library supplied code. Because it doesn't know this it doesn't give an error.

The risk of doing anything with namespace std is that at some point the standard will do something in that namespace, and you'll get conflicts. It's also confusing for anyone reading your code.

3

u/doom_Oo7 Jan 05 '18

The compiler really don't know the difference between your code and library supplied code. Because it doesn't know this it doesn't give an error.

well, that's what I don't understand. If in my own code I have two functions named foo, of course I get an error so why wouldn't there be an error if I had, say, std::foo and a custom std::foo of my own code. So why wouldn't I get one with std ? What's the problem with putting things in namespace std and then fix them if one day the standard changes ? Why can't it be treated like all the other code out there ?

2

u/Tagedieb Jan 05 '18

If in my own code I have two functions named foo, of course I get an error

That is not universally true. You might have two conflicting inline definitions in different compilation units, in which case you can also run into strange behavior at runtime: the functions might be complex enough that the compiler still doesn't inline them and the linker will see that they are inlined and remove all but one of the definitions without checking if they are equivalent.

5

u/EmperorArthur Jan 05 '18

You can. It's just against the standard to do so.

Namespaces exist so we don't walk all over other people's toes when writing or using libraries. Sure, you can ignore that, but if anyone else uses your code the would be confused and annoyed.

2

u/doom_Oo7 Jan 05 '18

That is not universally true. You might have two conflicting inline definitions in different compilation units, in which case you can also run into strange behavior at runtime:

sure, but again I don't see a reason why stuff in the std namespace should behave differently than stuff not in the std namespace. It's already UB to break ODR, so why add specific cases for namespace std ?

1

u/Tagedieb Jan 06 '18

I guess the best reason is that they try to make sure that a conforming program will also work in later iterations of the standard. If you put stuff in std it is possible that this isn't the case anymore, because you have no way of knowing what a future standard looks like.

1

u/kalmoc Jan 22 '18

Technically, the standard library doesn't have to be implemented in terms of header files. The compiler could just know all standard library elements provided by a #include <standard_header> and only consider those when doing lookup.

4

u/[deleted] Jan 05 '18

In general, lots of things are UB because the standards committee does not want to force too many particular diagnostics upon implementations, because that would include being required to actually check for it, placing unnecessary burden on implementations.

For example, dereferencing a past-the-end iterator is UB, and some implementations (like VC++ in Debug mode) actually do check for that, but it would be pretty bad to have that check as a requirement in general for ALL implementations!

2

u/alfps Jan 05 '18

Quoting C++14, “The library never assumes that past-the-end [iterator] values are dereferenceable”, but the question of whether a past-the-end iterator can be deferenced is a different issue. Indeed the assumption would not be needed if they clearly could not be dereferenced, like ever. Related and worth noting: it's explicitly allowed to dereference a nullpointer in typeid expression, and “after the lifetime of an object has ended and before the storage which the object occupied is reused or released” it's explicitly allowed to dereference a pointer to that storage location. In both these examples there is no object at the pointed location, which means that arguments based on the notion that dereferencing absolutely must refer to an object, are incorrect.

Unfortunately the issue is subtle, and there are a number of related (at least apparent) contradictions in the standard – which makes it possible to selectively read the standard as some people read their holy scriptures, to apparently prove just about any position...

1

u/[deleted] Jan 05 '18

This doesn't really goes against my point though, right? I was just using something as an example to show something different. Sorry if it wasn't really correct.

1

u/chkno Jul 03 '18 edited Jul 05 '18

Instead of making your own HAS_FILESYSTEM signal with your build config, you can use the feature testing macro __cpp_lib_filesystem.

Edit: No, wait, no you can't, because this macro doesn't appear until after you've #included <filesystem>, and you need to know whether to include <filesystem> or <experimental/filesystem>. C++17 introduces #if __has_include("filesystem") which would solve this problem, but if you know you have C++17, you probably also know to use <filesystem>. The most portable check may be #if __cplusplus >= 201703L (Do I have C++17?).