Search code examples
c++stdstringostreamstd-pairostringstream

Std::pair/ostringstream ctor syntax


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.


Solution

  • The operator<< function you're using to write the string literals takes a basic_ostream& and returns a basic_ostream&. But str is a member of basic_ostringstream, so you cannot invoke it on a basic_ostream&.

    You could cast the return value, and your code would compile

    static_cast<std::ostringstream&>(std::ostringstream().flush() << "hello")).str()
    

    Also, since flushing a default constructed stringstream is pointless, you should get rid of that.