Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:10:30+00:00 2026-06-11T16:10:30+00:00

Is it possible to write a method that takes a stringstream and have it

  • 0

Is it possible to write a method that takes a stringstream and have it look something like this,

void method(string str)
void printStringStream( StringStream& ss)
{
    method(ss.str());
}

And can be called like this

stringstream var;
printStringStream( var << "Text" << intVar << "More text"<<floatvar);

I looked up the << operator and it looks like it returns a ostream& object but I’m probably reading this wrong or just not implementing it right.

Really all I want is a clean way to concatenate stuff together as a string and pass it to a function. The cleanest thing I could find was a stringstream object but that still leaves much to be desired.

Notes:

I can’t use much of c++11 answers because I’m running on Visual Studio 2010 (against my will, but still)

I have access to Boost so go nuts with that.

I wouldn’t be against a custom method as long as it cleans up this mess.

Edit:

With @Mooing Duck’s answer mixed with @PiotrNycz syntax I achieved my goal of written code like this,

try{

    //code

}catch(exception e)
{   
    printStringStream( stringstream() << "An exception has occurred.\n"
                            <<"    Error: " << e.message 
                            <<"\n If this persists please contact "<< contactInfo
                            <<"\n Sorry for the inconvenience");
}

This is as clean and readable as I could have hoped for.

Hopefully this helps others clean up writing messages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:10:31+00:00Added an answer on June 11, 2026 at 4:10 pm

    Ah, took me a minute. Since operator<< is a free function overloaded for all ostream types, it doesn’t return a std::stringstream, it returns a std::ostream like you say.

    void printStringStream(std::ostream& ss)
    

    Now clearly, general ostreams don’t have a .str() member, but they do have a magic way to copy one entire stream to another:

    std::cout << ss.rdbuf();
    

    Here’s a link to the full code showing that it compiles and runs fine http://ideone.com/DgL5V

    EDIT

    If you really need a string in the function, I can think of a few solutions:

    First, do the streaming seperately:

    stringstream var;
    var << "Text" << intVar << "More text"<<floatvar;
    printStringStream(var);
    

    Second: copy the stream to a string (possible performance issue)

    void printStringStream( ostream& t)
    {
        std::stringstream ss;
        ss << t.rdbuf();
        method(ss.str());
    }
    

    Third: make the other function take a stream too

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write a generalised orElse method from Option that takes a
I'm trying to write unit tests for a method that takes a String filename,
I am trying to write a method in C# that takes a string with
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I know that TDD helps a lot and I like this method of development
I would like to write a method that take a Closure as argument and
I'm not sure if something like this is even possible in Python, but if
I was wondering how you would write a method in Scala that takes a
Is it possible to write a method in Scala which returns an object of
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.