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 917191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:10:19+00:00 2026-05-15T18:10:19+00:00

The following segment demonstrates my issue: (compilation error on GCC) stringstream ss; string s;

  • 0

The following segment demonstrates my issue: (compilation error on GCC)

stringstream ss;
string s;
ss << "Hello";

// This fails:
// s.swap(ss.str());

// This works:
ss.str().swap(s);

My error:

constSwap.cc:14: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::swap(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
basic_string.tcc:496: note: candidates are: void std::basic_string<_CharT, _Traits, _Alloc>::swap(std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

While I understand that str() in stringstream returns a temporary, it does not make sense and was not immediately apparent that I should have been calling the swap on the temporary with the local variable as parameter instead of my first instinct.

Obviously straight assignment works better, and newer C++ standards have move semantics which is perfect, but these are not available for my implementation.

Visual Studio does not give this problem due to it being relaxed about the C++ standard. I kinda already understand the whole const reference to a temporary thing (which I assume is the reason for my compilation errors).

My question:
Can anyone explain to me if this is the only solution, and perhaps explain to me the how to think about this in future so I can spot and work around similar issues?

(If no-one has any great insights I’m at least posting this here for people with similar issues)

  • 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-05-15T18:10:19+00:00Added an answer on May 15, 2026 at 6:10 pm

    After having used the swap-with-temporary idiom enough times, with lines like

    std::vector<int>().swap(v); // clear and minimize capacity
    

    or

    std::vector<int>(v).swap(v); // shrink to fit
    

    this does not seem so out of place. It’s normal to call swap as a member function of a temporary object. Of course, it’s not so idiomatic to use swap to fill in a default-constructed string instead of using a copy constructor, as already mentioned.

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

Sidebar

Related Questions

I get the following error when I try to execute this code segment :
I have the following code segment in which I'm getting error using (var session
The following segment of code shows JOptionPane.showInputDialog() method. It works fine, Now I want
The following code segment demonstrates a recursive call using JavaScript. function timedCount() { document.getElementById('txt').value=c;
Need help in following code segment: static void Main(string[] args) { var customers =
With respect to the following code segment: struct Pair{ string name; double val; }
The default route file has the following segment: # This is a legacy wild
I have the following problem with this segment of code. Can some one please
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
I have two identical byte arrays in the following segment of code: /// <summary>

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.