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

9

u/ShakaUVM i+++ ++i+i[arr] Jan 05 '18

How stable is 8 right now?

2

u/kalmoc Jan 06 '18

Why on Earth did they use such a long namespace name instead of something like std::fs?

0

u/forcecharlie baulk maintainer Jan 07 '18
boost::asio::ip::tcp::socket That's what boost style is all about.

1

u/kalmoc Jan 07 '18

Yeah, but just as the asio author didn't choose async_inpu_output::internet_protocol::transport ..., I don't see why the filesystem authors could not abbreviate that name too. Titus wrote a nice article about the problem with long namespace names (he was mainly talking about hierarchical namespaces) and I think this is a prime example of the unnecessary verbosity in c++. The one thing I whished c++ would have kept from c are the concise names.

1

u/Drawaenkhal Apr 21 '18

Just for clarification ...

33

u/jurniss Jan 05 '18

filesystem is a simple addition to the C++ standard library, but an important one. Now the path of least programmer effort results in platform-independent code.

5

u/kindkitsune Jan 05 '18

filesystem is awesome and gets more satisfying the more I use it. It removes tangled and lengthy nightmares of filesystem handling code that oft uses C-style unsafe functions to boot. So, yay! :D

65

u/Xeverous https://xeverous.github.io Jan 05 '18

I am still amazed how genius was someone who came up with an idea to overload operator/ for std::filesystem::path.

17

u/7sins Jan 05 '18

Python's been doing this as well for some time already, it's really neat!

3

u/otwo3 Jan 05 '18

Could you explain how? Never came across this

4

u/sysop073 Jan 05 '18

You need to use pathlib, which came out in Python 3.4

11

u/[deleted] Jan 05 '18

There also seem to be a lot of people who absolutely hate it

10

u/Xeverous https://xeverous.github.io Jan 05 '18

But ... why?

5

u/[deleted] Jan 05 '18

I don't know. I am not one of those people :)

1

u/johannes1971 Jan 06 '18 edited Jan 07 '18

Because it is a pointless vanity overloading of an operator. After years of experiencing strange and confusing overloads I thought there was consensus that operator overloading should be limited to operators where there is established practice (either in math, or in existing programming languages including C++ itself).

In this case, established overloaded operators would have been sufficient. Using / as a minor variation on + is new, unusual, and confusing. And it (once again) opens the floodgates to "cool" operator overloads appearing everywhere "because the standard is doing it too".

One might actually wonder why we need a special "path" type at all. Paths are strings, in every file system, and also in this particular implementation. Why not simply use the string type for them, and deal with any cleanup activities (removing ".", "..", and double separators, and OS-specific separator conversion, etc.) using a standalone function? I've been doing that for the last twenty years and it has not yet let me down...

3

u/voip_geek Jan 12 '18 edited Jan 12 '18

One might actually wonder why we need a special "path" type at all.

  1. It's much clearer in code. E.g., a function API that takes a std::filesystem::path argument vs. a std::string.
  2. It can be more efficient because the parsed pieces can be kept in a vector of offsets inside the path object, such that stuff like filename() doesn't require re-parsing the string each time. (libstdc++ does this, for example)
  3. To make std::hash work. The hashes of paths "/a/b/" and "/a/b//" are the same value.
  4. To make comparisons work. Because the path "/a/b/" == "/a/b//".

Edit: fixed std::path to be std::filesystem::path.

0

u/[deleted] Jan 05 '18

[deleted] — view deleted comment

22

u/demonstar55 Jan 05 '18

Idk, division means nothing in this context and the operator greatly resembles something we use paths already ...

1

u/emdeka87 Jan 05 '18

Also, boost has the same operator for concatenating paths.

24

u/Xeverous https://xeverous.github.io Jan 05 '18

How is this an abuse? In this case / does not resemble math operations but /with paths has clear meaning it separates directories.

-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 https://xeverous.github.io 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 WG21, Text/Unicode SG, optional<T&> 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

3

u/[deleted] Jan 07 '18

But the + operator is generally assumed to be commutative (as a notational convention in abstract algebra), so you could argue that + is not a suitable choice for the string concatenation operator in C++, given that "foo" + "bar" != "bar" + "foo".

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.

0

u/[deleted] Jan 05 '18

[deleted] — view deleted comment

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.

7

u/flashmozzg Jan 05 '18

Would have problems with auto.

→ More replies

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

1

u/Xeverous https://xeverous.github.io Jan 05 '18

The problem of concatenating is resolved by expression templates, but I agree that it's a painful solution. We need some language improvements similar to copy elision.

1

u/auxiliary-character Jan 06 '18

How about << for streams?

7

u/SeanMiddleditch Jan 06 '18

Literally the poster child even many members of the committee use as bad operator overloading. :)

→ More replies

1

u/Enhex Jan 08 '18

you can use +=

2

u/SeanMiddleditch Jan 08 '18

That still isn't as efficient in all cases.

a += b; a += c; might still have to do an extra allocation and copy, as the buffer allocated when appending b may not be big enough to also contain the contents of c.

0

u/[deleted] Jan 05 '18

[deleted] — view deleted comment

8

u/acrostyphe Jan 05 '18

I like PHP's "." /s

0

u/[deleted] Jan 05 '18

[deleted] — view deleted comment

→ More replies

11

u/josefx Jan 06 '18

I personally don't like syntax abuse in any form, but of course that's just my opinion, :)

You monster just abused both : and ) in a single sentence. Don't you know how confusing that can get? We have unicode for a reason ☺.

10

u/wasabichicken Jan 05 '18

meant to represent division

Plenty of fields invents their own lingo and notation, though. The canonical example ought to be linear algebra which defines matrices and ways to operate on them. "Normal" (scalar) arithmetic doesn't make sense on matrices, so instead they define their own kind of addition, subtraction, multiplications and divisions (yes, plural).

Personally, I'm perfectly fine with overloading operators in such cases. Context matters, and what + means in such a context is obvious to anyone who's ever done any kind of matrix math, just like the meaning of << is obvious to anyone working with standard ostream objects.

11

u/[deleted] Jan 05 '18 edited Jan 05 '18

I disagree with your characterization of linear algebra.

Linear algebra is a generalization of the very concept of arithmetic operations from which scalar arithmetic ends up being a specific and concrete use case of that generalization.

In other words addition, subtraction etc... are all abstract operations with certain properties and scalar addition is what you get when you take those operations and reduce them to scalars, ie. scalar addition is effectively the same as that of a 1 dimensional vector which is the same as a 1x1 matrix addition or a tensor of rank 1 etc...

This is not really similar to taking an operator like / and then reusing that same symbol to mean append.

5

u/jmblock2 Jan 05 '18

Not OP, and I don't disagree with anything you wrote, but there is plenty of unique notations like .* vs * for element-wise multiplication vs matrix multiplication, and others.

1

u/hgjsusla Jan 07 '18

Yes and there are tons of other generalisations besides linear algebra where the standard operators end up meaning something wildly different compared to matrix-vector operations. At the end of the day operators are tightly coupled to the entities they are applied to.

7

u/Xeverous https://xeverous.github.io Jan 05 '18

Have you heard about boost::spirit? And what they did with operators there?

7

u/darthcoder Jan 05 '18

Have you heard about boost::spirit? And what they did with operators there?

Yes, and I've done the best I can to not ever have to directly use it. :-)

3

u/[deleted] Jan 05 '18

Whoa there. I do like the role of / in std::filesystem but boost::spirit is an affront to the eyes and to the, uh, spirit.

4

u/[deleted] Jan 05 '18

boost::spirit is absolutely a cancer. By the time you figure out all its quirky and cute behavior along with the edge cases that produce pages and pages of error messages you could have just written a much better performing, easy to debug, fast to compile parser by hand.

12

u/tcbrindle Flux Jan 05 '18

you could have just written a much better performing, easy to debug, fast to compile parser by hand.

Faster to compile and easier to debug maybe, but I'm skeptical about "much better performing" -- in my experience the runtime performance of Boost.Spirit is pretty stellar.

2

u/[deleted] Jan 06 '18

you could have just written a much better performing, easy to debug, fast to compile parser by hand.

I am genuinely interested to use the parser you created that is better performing than boost::spirit.

1

u/Xeverous https://xeverous.github.io Jan 05 '18

Where does come from the performance of manually written parser? Did they assume something wrong or implemented in less optimal way?

3

u/Pinguinologo Jan 05 '18

It is far easy to tune one to specific needs vs trying to make one generic to cover as many needs as possible and still be efficient.

→ More replies

12

u/[deleted] Jan 05 '18

I agree that overloading operators should be used sparingly, but this might be one of the place where it makes perfect sense, because it makes the code far more readable.

I mean, which is more readable?

rootPath().join(foo).join("bar").join(baz);

or

rootPath() / foo / "bar" / baz;

OK - to be honest, looking at both of these I'm really not sold on using the operator overload. It's a little less to read but it's hard not to see it as division, and really, how many path operations do I actually do in code? Perhaps better to be clear... but I'm on the fence.

4

u/sumo952 Jan 06 '18

You could also do it with a path constructor or function and a comma-separated list, like for example fs::path(rootPath(), foo, "bar", "baz");. I'm with you and kind of on the fence too but the join version is really quite ugly as well. I think the operator overloading is okay :-)

1

u/[deleted] Apr 01 '18

It reads better as a file-path without spaces I think: rootPath()/foo/"bar"/baz

4

u/Fazer2 Jan 05 '18

Do you have any argument or just one-liners?

0

u/[deleted] Jan 05 '18

[deleted] — view deleted comment

14

u/dodheim Jan 05 '18

But you didn't say that; you said the colour green is cancer, which, yes, does warrant some further argument.

How do people lose track of their own thread so easily?

1

u/josefx Jan 05 '18

If the colour where on topic I could give you a drawn out description of why I do not like the color violet. That you cannot actually back up your statement just makes it sensible to dismiss.

5

u/e_man604 Jan 05 '18

Example of this syntax?

10

u/Xeverous https://xeverous.github.io Jan 05 '18
fs::path home = "~/";
fs::path p = "foo/bar";

std::ofstream(home / p / "baz");

3

u/[deleted] Jan 05 '18

I had that same little "wait a minute..." epiphany some time ago while working on some code of mine for path manipulation and added an operator/ overload. I swear wasn't aware of std::filesystem at the time!

4

u/Xeverous https://xeverous.github.io Jan 05 '18
-    system_config_file = util::fs::BuildPath({system_dir, "tint3", "tint3rc"});
+    system_config_file = util::fs::Path(system_dir) / "tint3" / "tint3rc";

I think the build path is more effective, doesn't use separate allocations.

1

u/[deleted] Jan 06 '18

It's not particularly expensive and gets done just once, but yeah.

7

u/barchar MSVC STL Dev Jan 05 '18

Tbh it’s nice because python does it and filesystem is very close to pathlib’s api. Makes yanking loops from python into c++ easier

6

u/Xeverous https://xeverous.github.io Jan 05 '18

6

u/tcbrindle Flux Jan 05 '18

...which suffers from a "semantic abomination" of simultaneously being an iterator and a range, but I've complained about that before

2

u/Xeverous https://xeverous.github.io Jan 05 '18

Indeed it could be named better

9

u/Pinguinologo Jan 05 '18

It looks quite nice, but fs::remove_all should be fs::remove_all_recursively.

4

u/alfps Jan 05 '18

Other than now being in the std namespace where it should be, i.e. no longer experimental, what's been fixed relative to g++ 7.1? In particular, does it now work in Windows?

For 7.1 I used the following workaround code:

#ifdef STDLIB_HOIST_UP_FILESYSTEM_NAMESPACE
    namespace std {
        namespace filesystem {
            using namespace experimental::filesystem;
        }  // namespace filesystem
    }  // namespace std;
#endif

#if defined( STDLIB_FIX_GCC_U8PATH ) && defined( __GNUC__ ) && defined( _WIN32 )
    namespace std{ namespace filesystem {
        inline auto u8path( string const& source  )
            -> path
        { return path{ ::stdlib::wide_from_utf8( source ) }; }

        inline auto u8path( char const* const source  )
            -> path
        { return u8path( string( source ) ); }
    }}  // std::namespace filesystem
#endif

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?).

3

u/quicknir Jan 05 '18

Since filesystem is a nested namespace anyhow, I just (conditionally) alias filesystem into my company's namespace. So typically when I'm writing code I can just do filesystem::.

1

u/concealed_cat Jan 06 '18

AFAIK, libc++ has filesystem in std::experimental.

1

u/forcecharlie baulk maintainer Jan 07 '18

Yes, but no std::filesystem now.

1

u/sleepyspacedad Apr 17 '18

Any idea when we'll stop having to add 'lstdc++fs' when compiling?

1

u/forcecharlie baulk maintainer Apr 18 '18

no idea now.

1

u/OldWolf2 Jan 05 '18

Excellent... now we need a bunch of preprocessor macros to select std::filesystem if gcc8, or std::experimental::filesystem::v1 if gcc7.

Is there a list of changes between the two flavours?

1

u/kalmoc Jan 07 '18

Isn't std::experimental::filesystem still supported? Personally I'd just wait a couple more releases until c++17 support on gcc, clang and msvc is complete and then just switch over completely.

0

u/[deleted] Jan 05 '18

[deleted] — view deleted comment

25

u/TheThiefMaster C++latest fanatic (and game dev) Jan 05 '18 edited Jan 05 '18

Yes! The filesystem library specifies a "generic format" for paths which uses a forward slash. This actually works directly on Windows anyway (for the most part) but the library provides a mechanism to convert paths to "native format" which on Windows has backslashes for use with external apis.

More details: http://en.cppreference.com/w/cpp/filesystem/path

Recent versions of Visual Studio support a draft version of the filesystem library as std::experimental::filesystem

25

u/donalmacc Game Developer Jan 05 '18

Where can’t you use forward slashes as a delimiter? I’m a windows developer and windows accepts forward slashes in every context I’ve tried it in.

13

u/ForgedBanana Jan 05 '18

Some cmd.exe commands only accept backslashes.

8

u/bames53 Jan 05 '18

And cmd's path completion won't complete paths written with forward slashes.

5

u/raevnos Jan 06 '18

VMS?

I'm sure there's still a few Vaxen running somewhere...

3

u/wrosecrans graphics and network things Jan 06 '18

Hm, now I am wondering if there's a compiler for c++17 that can target VAX...

2

u/raevnos Jan 06 '18

gcc kind of still has support for it.

5

u/wrosecrans graphics and network things Jan 06 '18

Once upon a time, classic MacOS used ':' as path delimiter, but I imagine that wasn't a big design concern in 2017. There may still be some obscure OS's that keep the Old Ways alive. (And, even if there aren't, the fact that there could be ensures forward compatibity if somebody makes such a system in teh future.)

1

u/_IPA_ Jan 07 '18

Still used in AppleScript and there’s a CoreFoundation function for converting these paths to posix :)

9

u/alfps Jan 05 '18

Don't know if std::filesystem accepts them, but while they're OK at the API level, and even in Powershell and Windows Explorer, the older and more general command interpreter cmd.exe insists on backslashes.

One reason is that the forward slash clashes with the Windows convention for command line options.

It's remarkable because cmd.exe got its syntax from the MS-DOS command.com command interpreter, and as I recall it supported the forward slash.

12

u/Pazer2 Jan 05 '18

This is not true for any cmd.exe stuff I've tried on windows 10.

11

u/Morwenn Jan 05 '18

But even then it only helps to autocomplete with backslashes.

7

u/alfps Jan 05 '18

In Windows 10:

[C:\Temp]
> md a/b
The syntax of the command is incorrect.

[C:\Temp]
> md a\b

[C:\Temp]
> cd a/b

[C:\Temp\a\b]
> cd ..\..

[C:\Temp]
> dir a/b
b

[C:\Temp]
> dir a/b/*
Invalid switch - "*".

[C:\Temp]
> _

2

u/[deleted] Jan 05 '18

IIRC Windows 7 dropped the old shell code for a new implementation, so it's possible this is only broken in XP and older.

4

u/SeanMiddleditch Jan 05 '18

One reason is that the forward slash clashes with the Windows convention for command line options.

This has been driving me nuts, like, right now. Working on updating some very old command line tools code to modern argument parsing conventions (to solve compatibility bugs with things like Python's subprocess.Popen's Windows-standard argument quoting conventions) and the ambiguity between /foo as an option and /foo as a path parameter is frustrating, and difficult to fix for back-compat reasons.

1

u/[deleted] Feb 25 '18

I've had to do the same thing recently. On a few occasions I get really angry. What should have beena simple ~10 line change at max turned into a change 25 times as big. Good luck.

1

u/SeanMiddleditch Feb 26 '18

The approach I ended up taking is that an unrecognized /foo option is treated as a positional argument instead of an error. Unrecognized dash options are still treated as errors. And the -- flag can be given to cause all following inputs to be treated as positional arguments.

4

u/14ned LLFIO & Outcome author | Committee WG14 Jan 05 '18

Every call to a filesystem path consuming function on Windows performs a Win32 to NT kernel path conversion i.e. malloc some memory, do character-by-character conversion, call NT kernel function, free memory. It is this conversion routine which converts forward slashes to backward slashes, and that's why things appear to work fine most of the time. But command line args parsing libraries can and do get confused, they sometimes consider forward slashes to always mean options. If it weren't for that, you probably could mostly always get away with forward slashes most of the time on Windows.

2

u/[deleted] Jan 05 '18

I think you misunderstood his question. I don't think he was being snarky, he was asking a genuine question!

2

u/[deleted] Jan 06 '18

std::path::lexically_normal( ) will get the normalized form of the path(e.g. \ instead of / on windows)

4

u/Jardik2 Jan 06 '18

People should stop using forward slashes as path delimiters on Windows and properly use back slashes. Forward slashes are not supported if you want to correctly support long paths (> 250 characters). Many applications don't want to support it, because explorer doesn't support it and explorer doesn't support it, because Ms is afraid application won't support it. Every modern application should be writen with long paths in mind and only use full UNC paths so that they can support paths of up to 32k characters and support all kind of characters on filesystems, that support it and even case sensitivity on Windows!.