Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

iostream

stl

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?

like image 994
vy32 Avatar asked Apr 26 '10 21:04

vy32


1 Answers

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.

like image 100
sbi Avatar answered Sep 22 '22 23:09

sbi