Why does the following code...
#include <iostream>
#include <sstream>
#include <utility>
int main()
{
std::pair<std::string, std::string> pair1((std::ostringstream().flush() << "hello").str(), (std::ostringstream().flush() << "world").str());
return 0;
}
... generate the compiler error...
>g++ main.cpp
main.cpp: In function ‘int main()’:
main.cpp:7: error: ‘struct std::basic_ostream<char, std::char_traits<char> >’ has no member named ‘str’
main.cpp:7: error: ‘struct std::basic_ostream<char, std::char_traits<char> >’ has no member named ‘str’
...whereas the code below does not generate any compiler error?
#include <iostream>
#include <sstream>
#include <utility>
int main()
{
std::ostringstream oss1;
std::ostringstream oss2;
oss1 << "hello";
oss2 << "world";
std::pair<std::string, std::string> pair1(oss1.str(), oss2.str());
return 0;
}
If anyone can advise how to, I would ideally like to pull off a "one-line" creation and population of my std::pair, as I'm attempting to do in the first code block. Thank you.