tags:

views:

440

answers:

3

This is a complaint about STL. Why do they take filename arguments as (char *) and not as std::string? This seems to make no sense.

There are two other questions on this topic:

The issue is that I have a lot of code that looks like this:

    std::ofstream f(fname.c_str());

WhenI would like it to look like this:

std::ofstream f(fname);

Additional issues that are mentioned in the above posts is the issue of UTF-16 vs. UTF-8. (UTF-16 might contain NULLs which would break the POSIX API). But that's not really an issue, because the implementation could convert UTF-16 to UTF-8 before calling open().

But seriously, this makes no sense. Are there any plans to upgrade STL?

+9  A: 

why don’t ifstream and ofstream classes take std::string as filenames?

I've seen a few sensible arguments for that (namely that this would create a dependency of the streams on strings), but frankly I believe the actual reason is that the streams are much older than the standard library and its strings.

Are there any plans to upgrade STL?

It's called C++11 and will be the new version of the standard. I don't know whether file streams changed. You could look at the final draft and find out for yourself.

Note that STL is the name for a library of containers, algorithms, and iterators, incorporated into the standard library. Also part of the standard library are strings, streams and others.
In particular, streams are not part of the STL. They are siblings.

sbi
A quick look at the draft shows that `string` will allowed as a filename but `wstring` will not.
Potatoswatter
Thank you. Well, that's less brain-dead.
vy32
@Potatoswatter: surely you mean "`string` will be mandatory, but `wstring` is an extension". C++98 allowed extra overloads, and I don't recall a change there?
MSalters
@MSalters: Well, GNU in any case does not have such an extension, and Mac OS X in any case doesn't provide a syscall `wopen` accepting `wchar_t*`. So it looks like UTF-8 is the portable way to go.
Potatoswatter
+2  A: 

Historical reasons. The iostream library was developed separately from the string stuff. But why this wasn't integrated in the C++ Standard is anyone's guess. I've seem several questions on Usenet way back when (including the dependancy theory) , but never a really satisfactory explanation.

anon
+2  A: 

As I recall, it actually is (at least sort of) the situation with string vs. wstring. I can't find the post right now, but I'm reasonably certain I remember a Usenet post by Andrew Koenig saying that it had been brought up by members of one of the national committees (Japan is what I seem to recall, but could easily be wrong) brought up the question of how they could deal with file names in various languages (especially since relatively few OSes at the time provided much support for that). Even though it had started out pretty simple, it quickly became apparent that the only way to avoid it turning into a huge mess was to cease all discussion of the idea in general.

Jerry Coffin