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

  • Home
  • SEARCH
  • 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 7702861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:19:30+00:00 2026-05-31T23:19:30+00:00

I wrote an in-place permutation algorithm [an exercise in TAOCP3], where the inner loop

  • 0

I wrote an in-place permutation algorithm [an exercise in TAOCP3], where the inner loop is

template<typename T>
void inplace_permute(T *pT, int *P, const int n)
{
  // part of the inner loop
  pT[j] = std::move(pT[k]);
  P[j] = j;

  // more logic to update j, k, etc.
}

Here, pT is the array of elements to be sorted, P is the permutation table and n is the number of elements.

Will std::move increase performance if T is a complex type, e.g., a string? Also important, can it be optimized out if T is a primitive type (e.g., an int?)

  • 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-31T23:19:31+00:00Added an answer on May 31, 2026 at 11:19 pm

    I’ll sum up your question as:

    What does std::move do ?

    Basically, std::move makes the use of Move Constructor and Move Assignment Operator available.

    Typically, those are close to a bitwise copy (they are not exactly one), and so the performance is usually related to the sizeof the class.

    Therefore, std::move(someint) and std::move(somestring) would have similar performance if they had a similar size, even though one is a built-in and the other a user class.

    There are some differences though.

    • on a built-in, a move is just a bitwise copy. Because the moved-from value is unspecified, no zeroing is required. You may want, after moving, to assign it a known value (0 or whatever)
    • on a user class, which typically has resources (such a dynamically allocated buffer), moving implies: cleaning the moved-to instance (for assignment), making a somewhat bitwise copy, resetting the moved-from instance. So there is a bit more work.

    To understand, we can illustrate this with an example string implementation:

    class String {
    public:
      // Many things
      String(String&& right);
      String& operator=(String right);
    
      friend void swap(String& left, String& right);
    
    private:
      // On 64 bits platform, 4x as big as an `int`
      size_t capacity;
      size_t size;
      char* buffer;
    };
    
    // Move Constructor
    String::String(String&& right):
       capacity(right.capacity), size(right.size), buffer(right.buffer)
    {
      right = String(); // reset right
    }
    
    
    // Assignment Operator
    String& String::operator=(String right) {
      swap(*this, right);
      return *this;
    }
    
    // Swap
    void swap(String& left, String& right) {
      using std::swap;
      swap(left.capacity, right.capacity);
      swap(left.size    , right.size);
      swap(left.buffer  , right.buffer);
    }
    

    As you can see, the assignment pT[j] = std::move(pT[k]); means (semantically):

    • creating a temporary (make a bitwise copy of pT[k])
    • reset pT[k]
    • exchange the state between the temporary and pT[j]
    • destroy the temporary (which typically release the storage inherited from pT[j])

    The compiler should, more or less, be able to optimize it into:

    • exchange the state between pT[j] and pT[k]
    • destroy pT[k] (just release the storage)
    • rebuild a new instance in pT[k]

    Or, crudely:

    swap(ptj, ptk);    // swap 3 fields
    delete ptk.buffer; // might be a no-op
    ptk = String();    // 0-out 3 fields
    

    Note: this is a toy implementation, it would be a bit more simple on gcc and much more complex on VC++ as they use different representations of the data.

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

Sidebar

Related Questions

I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote some xml but still cant place the text on the center of
I wrote this sql statement: SELECT e.id AS event_id, e.title, e.date, e.category, e.place, e.minAge,
I wrote class with template. class has map as member and some getxxx() /
I need to place 3 buttons and one textfield in UIAlertview. I wrote code
In order to put in place a continuous integration system, Hudson, I wrote a
I wrote a method Assert(): [System.Diagnostics.Conditional(DEBUG)] internal static void Assert(bool condition) { if (!condition)
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a custom URL rewriting module, to take certain paths and map them
I wrote a small internal web app using (a subset of) pylons . As

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.