I wrote some code and made a programming mistake, but it still worked. Later, when looking over the code I said to myself, "That shouldn't have compiled!".
I am running Microsoft Visual C++ 2010 Express.
Consider the following minimal code:
std::string filespec = "D:/Temp/Test.txt";
std::ofstream outFile( filespec );
outFile << "Hello, World!\n";
The ofstream constructor should have been
std::ofstream outFile( filespec.c_str() );
A std::string will also work with ofstream open() method.
Microsoft's documentation does not show this, either for the constructor or the open method. Nor is it listed as a language extension.
For the open() method Intellisense shows eight methods including const std::string&
and const std::wstring&
I couldn't make Intellisense show me the available constructors, but clearly a constructor with const std::string
I am not a professional programmer and don't have access to the C++ Standard.
Is this non-standard behavior or is this perhaps part of new Standard. I believe I would get compile time errors with VS C++ 2008 Express if I accidently passed a string instead of a char* as a parameter. Any comments?