element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ moving and returning objects part 2: move and copy (embedded friendly C++)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 20 Jul 2025 11:25 AM Date Created
  • Views 182 views
  • Likes 4 likes
  • Comments 1 comment
  • Modern C++
  • c++26
  • stl
  • c++23
  • c++
  • c++20
Related
Recommended

C++ moving and returning objects part 2: move and copy (embedded friendly C++)

Jan Cumps
Jan Cumps
20 Jul 2025

In object oriented designs, there are times when you replace an object by another. Example: when you store an object into an STL container. In a lot of those cases, you create an object, and add it. The object in the container is a copy of the object that you want to store. And then you discard the original object:

my_insurrance_array[2] = insurance("car");

In essence, it's a move. The object replaces the existing object in the array, and the temporary object is discarded. Standard, C++ does this by copying the value of each non-static data member from the temporary object into those of the existing object in that array. And for classes that have small data members (numbers, bools, ...) this is the fastest solution.

But once your class has larger data members, such as strings, containers, it becomes more expensive (for memory and clock ticks) to copy all that data over. To deal with that, C++ has introduced a move constructor, and the move assignment operator.

T::T(T&& rhd) noexcept : value(rhd.value), s(std::move(rhd.s)) { // move contructor
    cout << "move ctor" << endl;
}

T& T::operator=(T&& rhd) noexcept { // move assignment operator
    value = rhd.value;
    s = std::move(rhd.s);
    cout << "move =" << endl;
    return *this;
}

move constructor

If you construct a new object from an existing one, and that original one goes out of scope, C++ will select the move constructor. Here's an example of code that can (and will) invoke the move constructor:

T move_to(T t) {
    return t;
}

int main() {
    // ...
    T u = move_to(T());

This function receives an object as pass-by-value. So it's an object on its own. Then we return it to a calling application, where it will be received into a variable.
What C++ does, if you have a move constructor: it will use that constructor to create the object in place in the receiving variable. And use your move code to move the string member from the parameter object to the new object. You "transfer the ownership of that string member from parameter t to the newly constructed object. The parameter object is then destructed. 

image

move assignment operator

If you move an object into the place of another, C++ can (and will, if you provided it) call the move assignment operator. And use it's capability to transfer ownership of expensive data members to the receiving object. An example is, to assign an object to a particular location in an stl array:

    std::array<T, 4> array_of_t;
    array_of_t[0] = T();

The temporary object T() is moved into the container. The object at array_of_t[0] gets the ownership of T().s. Without copying the data over. That T() object is then destructed.

image

takeaway: my examples are somewhat artificial, but that's often the case when you try to show a mechanism. But the summary is: if you have an object with expensive data members, it makes sense to provide a move constructor and a move assignment operator. Whenever the compiler has the possibility, it 'll use those to transfer an existing object to a new location. And do that cheaper as when you only have a (default) copy constructor.

full code of blog 1 and 2: 

#include <array>
#include <iostream>

using std::cout;
using std::endl;

struct T {
    int value;
    std::string s;

    T();                                                                // ctor
    ~T();                                                               // dtor
    T(T&&) noexcept;                                                    // move ctor
    T(T const&);                                                        // copy ctor
    
    T& operator=(T&&) noexcept;                                         // move assignment
    T& operator=(T const&);                                             // copy assignment
};

T::T() : value(0), s("this is an attribute that's expensive to copy") { cout << "ctor" << endl; }
T::~T() { cout << "dtor" << endl; }

T::T(T&& rhd) noexcept : value(rhd.value), s(std::move(rhd.s)) {
    cout << "move ctor" << endl;
}

T::T(T const& rhd) { 
    value = rhd.value;
    s = rhd.s;
    cout << "copy ctor" << endl;
}

T& T::operator=(T&& rhd) noexcept { 
    value = rhd.value;
    s = std::move(rhd.s);
    cout << "move =" << endl; 
    return *this;
}

T& T::operator=(T const& rhd) { 
    value = rhd.value;
    cout << "copy =" << endl; 
    return *this;
}

T work() {
    return T();
}

void use() {
    auto obj = work();
}

T move_to(T t) {
    return t;
}

int main() {
    T t = work();
    T v();
    T u = move_to(T());
    T w = move_to(u);
    T z = std::move(t); // move-constructs from xvalue
    z = u;
    use();

    std::array<T, 4> array_of_t;
    array_of_t[0] = T();
    return 0;
}

Inspiration: MC++: The Rule of Zero, or Six and artificial::mind blog: Moves in Returns, cppreference Move constructors.

Thank you for reading.

 C++ moving and returning objects part 1: object as return value (embedded friendly C++) 

 C++ moving and returning objects part 2: move and copy (embedded friendly C++) 

  • Sign in to reply
  • DAB
    DAB 2 months ago

    Nice post Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube