<p>This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ <code>std::fstream</code> classes don't take a <code>std::string</code> in their constructor or open methods. Everyone loves code examples so:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;

int main()
{
    std::string filename = "testfile";      
    std::ifstream fin;

    fin.open(filename.c_str()); // Works just fine.
    fin.close();

    //fin.open(filename); // Error: no such method.
    //fin.close();
}
</code></pre>

<p>This gets me all the time when working with files. Surely the C++ library would use <code>std::string</code> wherever possible?</p>

[{'answer_id': 32353, 'author': 'Magnus Westin', 'author_id': 2957, 'author_profile': 'https://Stackoverflow.com/users/2957', 'pm_score': 0, 'selected': False, 'text': "<p>Is there any class in STL that takes a string... I dont think so (couldnt find any in my quick search). So it's probably some design decision, that no class in STL should be dependent on any other STL class (that is not directly needed for functionality).</p>\n"}, {'answer_id': 32368, 'author': 'Christopher', 'author_id': 3186, 'author_profile': 'https://Stackoverflow.com/users/3186', 'pm_score': 4, 'selected': False, 'text': '<p>There are several places where the C++ standard committee did not really optimize the interaction between facilities in the standard library.</p>\n\n<p><code>std::string</code> and its use in the library is one of these.</p>\n\n<p>One other example is <code>std::swap</code>. Many containers have a swap member function, but no overload of std::swap is supplied. The same goes for <code>std::sort</code>.</p>\n\n<p>I hope all these small things will be fixed in the upcoming standard.</p>\n'}, {'answer_id': 32406, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 0, 'selected': False, 'text': "<p>I believe that this has been thought about and was done to avoid the dependency; i.e. #include &lt;fstream&gt; should not force one to #include &lt;string&gt;.</p>\n\n<p>To be honest, this seems like quite an inconsequential issue.  A better question would be, why is std::string's interface so large?</p>\n"}, {'answer_id': 32442, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>It is inconsequential, that is true. What do you mean by std::string's interface being large? What does large mean, in this context - lots of method calls? I'm not being facetious, I am actually interested.</p>\n</blockquote>\n\n<p>It has more methods than it really needs, and its behaviour of using integral offsets rather than iterators is a bit iffy (as it's contrary to the way the rest of the library works).</p>\n\n<p>The real issue I think is that the C++ library has three parts; it has the old C library, it has the STL, and it has strings-and-iostreams.  Though some efforts were made to bridge the different parts (e.g. the addition of overloads to the C library, because C++ supports overloading; the addition of iterators to basic_string; the addition of the iostream iterator adaptors), there are a lot of inconsistencies when you look at the detail.</p>\n\n<p>For example, basic_string includes methods that are unnecessary duplicates of standard algorithms; the various find methods, could probably be safely removed. Another example: locales use raw pointers instead of iterators.</p>\n"}, {'answer_id': 37542, 'author': 'wilhelmtell', 'author_id': 456, 'author_profile': 'https://Stackoverflow.com/users/456', 'pm_score': 6, 'selected': True, 'text': '<p>By taking a C string the C++03 <a href="http://en.cppreference.com/w/cpp/io/basic_fstream" rel="noreferrer"><code>std::fstream</code></a> class reduced dependency on the <code>std::string</code> class. In C++11, however, the <code>std::fstream</code> class does allow passing a <code>std::string</code> for its constructor parameter.</p>\n\n<p>Now, you may wonder why isn\'t there a transparent conversion from a <code>std:string</code> to a C string, so a class that expects a C string could still take a <code>std::string</code> just like a class that expects a <code>std::string</code> can take a C string.</p>\n\n<p>The reason is that this would cause a conversion cycle, which in turn may lead to problems.  For example, suppose <code>std::string</code> would be convertible to a C string so that you could use <code>std::string</code>s with <code>fstream</code>s.  Suppose also that C string are convertible to <code>std::string</code>s as is the state in the current standard.  Now, consider the following:</p>\n\n<pre><code>void f(std::string str1, std::string str2);\nvoid f(char* cstr1, char* cstr2);\n\nvoid g()\n{\n    char* cstr = "abc";\n    std::string str = "def";\n    f(cstr, str);  // ERROR:  ambiguous\n}\n</code></pre>\n\n<p>Because you can convert either way between a <code>std::string</code> and a C string the call to <code>f()</code> could resolve to either of the two <code>f()</code> alternatives, and is thus ambiguous.  The solution is to break the conversion cycle by making one conversion direction explicit, which is what the STL chose to do with <code>c_str()</code>.</p>\n'}, {'answer_id': 62852, 'author': 'swmc', 'author_id': 7016, 'author_profile': 'https://Stackoverflow.com/users/7016', 'pm_score': 2, 'selected': False, 'text': '<p>@ Bernard:<br>\nMonoliths "Unstrung." "All for one, and one for all" may work for Musketeers, but it doesn\'t work nearly as well for class designers. Here\'s an example that is not altogether exemplary, and it illustrates just how badly you can go wrong when design turns into overdesign. The example is, unfortunately, taken from a standard library near you...\n~ <a href="http://www.gotw.ca/gotw/084.htm" rel="nofollow noreferrer">http://www.gotw.ca/gotw/084.htm</a></p>\n'}, {'answer_id': 62981, 'author': 'Pieter', 'author_id': 5822, 'author_profile': 'https://Stackoverflow.com/users/5822', 'pm_score': 4, 'selected': False, 'text': "<p>Maybe it's a consolation: all fstream's have gotten an open(string const &amp;, ...) next to the open(char const *, ...) in the working draft of the C++0x standard.\n(see e.g. 27.8.1.6 for the basic_ifstream declaration)</p>\n\n<p>So when it gets finalised and implemented, it won't get you anymore :)</p>\n"}, {'answer_id': 73620, 'author': 'Drealmer', 'author_id': 12291, 'author_profile': 'https://Stackoverflow.com/users/12291', 'pm_score': 3, 'selected': False, 'text': '<p>The stream IO library has been added to the standard C++ library before the STL. In order to not break backward compatibility, it has been decided to avoid modifying the IO library when the STL was added, even if that meant some issues like the one you raise.</p>\n'}, {'answer_id': 13326739, 'author': 'doug65536', 'author_id': 1127972, 'author_profile': 'https://Stackoverflow.com/users/1127972', 'pm_score': 1, 'selected': False, 'text': '<p>C++ grew up on smaller machines than the monsters we write code for today. Back when iostream was new many developers really cared about code size (they had to fit their entire program and data into several hundred KB). Therefore, many didn\'t want to pull in the "big" C++ string library. Many didn\'t even use the iostream library for the same reasons, code size.</p>\n\n<p>We didn\'t have thousands of megabytes of RAM to throw around like we do today. We usually didn\'t have function level linking so we were at the mercy of the developer of the library to use a lot of separate object files or else pull in tons of uncalled code. All of this FUD made developers steer away from std::string.</p>\n\n<p>Back then I avoided std::string too. "Too bloated", "called malloc too often", etc. Foolishly using stack-based buffers for strings, then adding all kinds of tedious code to make sure it doesn\'t overrun.</p>\n'}, {'answer_id': 35925171, 'author': 'Jonathan Birge', 'author_id': 787906, 'author_profile': 'https://Stackoverflow.com/users/787906', 'pm_score': 0, 'selected': False, 'text': '<p>Nowadays you can solve this problem very easily: add <code>-std=c++11</code> to your <code>CFLAGS</code>.</p>\n'}]
['https://Stackoverflow.com/questions/32332', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/61/']