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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:03:54+00:00 2026-06-10T19:03:54+00:00

I’m trying to learn move semantics well enough to introduce it to my students.

  • 0

I’m trying to learn move semantics well enough to introduce it to my students. I’ve been using highly simplified vector- or string-like classes that manage memory and whose members output messages to demonstrate their activity. I’m trying to develop a simple set of examples to show students.

Construction elision for RVO and elsewhere in gcc 4.7 and clang aggressively eliminates copy and move construction, so while I can easily see move assignment at work, the only time I’ve seen move construction at work is if I turn off construction elision in gcc 4.7 with -fno-elide-constructors.

An explicit copy construction statement

MyString newString(oldString);

will invoke the copy constructor even if elision is enabled. But something like

MyString newString(oldString1 + oldString2); 

doesn’t invoke the move constructor because of the elision.

Anything explicitly using std::move won’t make a simple example because explaining std::move has to come later.

So my question: Is there a simple code example that will invoke move construction even if copy/move constructors are being elided?

  • 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-10T19:03:56+00:00Added an answer on June 10, 2026 at 7:03 pm

    The simple example would be an argument to a function that is returned. The standard explicitly forbids eliding the move in this case (not that they could…):

    std::vector<int> multiply( std::vector<int> input, int value ) {
       for (auto& i : input )
          i *= value;
       return input;
    }
    

    Additionally, you can explicitly request move construction for an even simpler although a bit more artificial example:

    T a;
    T b( std::move(a) );
    

    Uhm… yet another that does not involve std::move (it can technically be elided, but most compilers will probably not):

    std::vector<int> create( bool large ) {
       std::vector<int> v1 = f();
       std::vector<int> v2 = v1;       // modify both v1 and v2 somehow
       v2.resize( v2.size()/2 );
       if ( large ) {
          return v1;
       } else {
          return v2;
       }
    }
    

    While the optimizer can elide it by rewriting the code as:

    std::vector<int> create( bool large ) {
       if ( large ) {
          std::vector<int> v1 = f();
          std::vector<int> v2 = v1;       // modify both v1 and v2 somehow
          v2.resize( v2.size()/2 );
          return v1;
       } else {
          std::vector<int> v1 = f();
          std::vector<int> v2 = v1;       // modify both v1 and v2 somehow
          v2.resize( v2.size()/2 );
          return v2;
       }
    }
    

    I pretty much doubt that the compiler will actually do it. Note that in each code path the object being returned in known before v1 and v2 are created, so the optimizer can locate the proper object in the return location after the rewrite.

    The situations where copy/move can be elided are described in 12.8/31. If you manage to write code that does not fall into those categories and the type has a move constructor, the move constructor will be called.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.