Is streaming a stringstream a libstdc++ extension? This program compiles with gcc-4.2, gcc-4.7-2 (using -std=c++03), and clang 3.2 using -std=c++11 and libstdc++ (thanks to Andy Prowl, see comments). It does not compile with clang 3.2 using -std=c++11 and -stdlib=libc++.
#include<iostream>
#include<sstream>
int main() {
std::stringstream s; s << "b";
std::cout << "ss: " << s << std::endl;
return 0;
}
By looking at the constructor of ofstream it can take a std::basic_streambuf<CharT, Traits>* or a basic_ostream& st. A stringstream is a std::basic_istream, however both are std::basic_ios<CharT, Traits> so I would guess it should work.
The following change makes the code compile under clang:
std::cout << "ss: " << s.str() << std::endl;
What is the right way to do it? cout << s; or cout << s.str(); ?
No, it’s a difference between C++03 and C++11. All streams have a conversion operator that enables code using
if (s)andwhile (s). In C++03 this is usuallyoperator void*()or something similar.In C++11 we have explicit operators, where an
explicit operator bool()works forif (s), but not forcout << s.