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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:57:26+00:00 2026-05-26T06:57:26+00:00

Is it actually guaranteed anywhere that the following reduce-capacity trick will work? int main()

  • 0

Is it actually guaranteed anywhere that the following reduce-capacity trick will “work”?

int main() {
   std::string s = "lololololol";
   s = "";                        // capacity still non-zero

   string(s).swap(s);             // ?
}

It doesn’t seem to “work” for me (in that the capacity remains non-zero), and I can’t find anything in the standard that says anything more than that the “contents” must be swapped between the two [here, identical] objects.

Similarly, for sequence containers:

int main() {
   vector<int> v { 1,2,3,4,5 };
   v.clear();                   // capacity still non-zero

   vector<int>(v).swap(v);      // ?
}

As far as I’m aware, this “trick” is semi-widely used; perhaps this widespread adoption is misguided?

(Of course, in C++11 we have shrink_to_fit [albeit non-binding] instead, making this kind of moot.)

  • 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-26T06:57:26+00:00Added an answer on May 26, 2026 at 6:57 am

    I’ve always been taught that there is no guaranteed standard way to lower the capacity. All methods have been (and still are) implementation defined.

    § 23.2.1\8 says:

    The expression a.swap(b), for containers a and b of a standard
    container type other than array, shall exchange the values of a and b
    without invoking any move, copy, or swap operations on the individual
    container elements…

    This guarantees that the internal pointers of vectors must be swapped.
    However, I cannot find anything that guarantee on the capacity of a newly created vector.

    § 21.4.2\1 says that one of the basic_string default constructor’s post conditions is that capacity() returns an unspecified value.
    § 21.4.2\3 says that one of the basic_string copy constructor’s post conditions is that capacity() returns a value at least as big as size().
    § 21.4.6.8\2 says that string::swap runs in constant time, which (effectively) requires that the internal pointers are swapped.

    As far as I can tell, a conforming implementation could have string::max_size() { return 4;}, and swapping all internals from one buffer to another would therefore be constant time. (vector can’t do that though)

    Obviously, take this all with a grain of salt. I’m quoting from the C++ draft from Feb28,’11, and I can’t find specifications for the vector’s copy constructor. Also, not finding evidence for is not the same as finding evidence against.

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

Sidebar

Related Questions

So I have the following test code: #include <string.h> #include <stdio.h> int main(int argc,
Assuming this is a singleton implementation: Am I guaranteed that this will only call
actually this is not hang status, i mean..it slow response, so in that case,
Actually, almost exactly the same thing. A matrix of small views that would expand
Actually We are doing thesis work where we need to make 10 voip phones
The docs vaguely say that it will stick around as long as needed, but
Running the following query using PDO (Actually, I use prepared statements but same problem)
I know that the .NET memory model (on the .NET Framework; not compact/micro/silverlight/mono/xna/what-have-you) guaranteed
Actually in the center of current view, above all other objects. Cross-browser and without
Actually I have two questions. (1) Is there any reduction in processing power or

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.