why don't STL ifstream and ofstream classes take std::string as filenames?
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:
- How to open unicode filenames with STL
- Windows Codepage interactions with C++
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 ab开发者_C百科ove 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?
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.
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.
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.
精彩评论