element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Forum testing C++ object copy vs move vs pass-on
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 0 replies
  • Subscribers 47 subscribers
  • Views 42 views
  • Users 0 members are here
  • c++
Related

testing C++ object copy vs move vs pass-on

Jan Cumps
Jan Cumps 6 days ago

I'm testing what constructs generate object creation, copy, move, or just pass-on. This post is a log of test code, trials, errors.

Test classes

#include <iostream>
#include <array>
#include <string>

template <class T> struct result {
    result() {                                                                                  // default constructor
        std::cout << "result<T> constructor\n";
    }
    T check;
    constexpr explicit operator bool() const noexcept { return static_cast<bool>(check); }
};

struct bool_class{
    bool_class() { std::cout << "bool_class constructor\n"; }                                   // default constructor
    bool_class(const bool_class& other) { std::cout << "bool_class copy constructor\n"; }       // copy constructor
    bool_class(bool_class&& other) noexcept { std::cout << "bool_class move constructor\n"; }   // move constructor

    bool_class& operator =(const bool_class& other) {                                           // copy assignment operator
        std::cout << "bool_class copy assignment operator\n";
        return *this;
    }
    bool_class& operator = (bool_class&& other) noexcept {                                      // move assignment operator
        std::cout << "bool_class move assignment operator\n";
        return *this;
    }

    constexpr explicit operator bool() const noexcept{ return true; }                           // status return, obviously test code
};

Test functions (object factory and similar)

using bool_result = result<bool_class>;

bool_result get_bool_result() { // factory method
    bool_result br; // calls default constructor and result<T> constructor
    return br;
}

bool_result get_bool_result(bool_class& bc) {
    bool_result br; // calls default constructor and result<T> constructor
    br.check = bc;  // calls copy assignment operator
    return br;
}

bool_result get_bool_result(bool_class&& bc) {
    bool_result br;             // calls default constructor and result<T> constructor
    br.check = std::move(bc);   // calls move assignment operator
    return br;
}

bool_result& get_bool_result(bool_result& br) { // calls nothing, zero cost
    return br;
}

bool_result&& get_bool_result(bool_result&& br) { // calls nothing, zero cost
    return br;
}

Test code

int main() {
    if ( auto&& [check] = get_bool_result()) {   // calls default constructor inside the function
        std::cout << "success\n";
        std::cout << (bool)check << "\n";

        bool_class a = check;                   // calls copy constructor
        // bool_class&& b = check;              // invalid
        bool_class c = std::move(check);        // calls move constructor
        bool_class&& d = std::move(check);      // calls nothing
        bool_class e(check);                    // calls copy constructor
        // bool_class&& f(check);               // invalid
        bool_class g(std::move(check));         // calls move constructor
        bool_class&& h(std::move(check));       // calls nothing

        bool_class i = a;                       // calls copy constructor
        // bool_class&& j = a;                  // invalid
        bool_class k = d;                       // calls copy constructor
        // bool_class&& l = d;                  // invalid
        bool_class m = std::move(a);            // calls move constructor
        bool_class&& n = std::move(a);          // calls nothing
        bool_class o = std::move(d);            // calls move constructor
        bool_class&& p = std::move(d);          // calls nothing

        a = check;                              // calls copy assignment operator
        d = check;                              // calls copy assignment operator
        e = check;                              // calls copy assignment operator
        g = std::move(check);                   // calls move assignment operator
        h = std::move(check);                   // calls move assignment operator

        std::array<bool_class,1> ar_a;          // calls copy constructor
        ar_a[0] = a;                            // calls copy assignment operator
        ar_a[0] = d;                            // calls copy assignment operator
        ar_a[0] = std::move(a);                 // calls move assignment operator
        ar_a[0] = std::move(d);                 // calls move assignment operator

        std::array<bool_class,1> ar_b = {a};                // calls copy constructor
        std::array<bool_class,1> ar_c = {check};            // calls copy constructor
        std::array<bool_class,1> ar_d = {std::move(a)};     // calls move constructor
        std::array<bool_class,1> ar_e = {std::move(check)}; // calls move constructor

        if ( auto&& [check] = get_bool_result(a)) {}            // calls copy assignment operator
        if ( auto&& [check] = get_bool_result(std::move(a))) {} // calls move assignment operator

        bool_result br; // calls default constructor and result<T> constructor
        if ( auto&& [check] = get_bool_result(br)) {}            // calls nothing, zero cost
        if ( auto&& [check] = get_bool_result(std::move(br))) {} // calls nothing, zero cost
    }
}

I documented the pieces of code that either called a constructor or an assignment operator. Both use some resources (just like creating or assigning to a variable cost something).

Some constructs have a comment // calls nothing. These don't use resources (no memory, cpu time, binary code size).

Requires GCC 15

  • Sign in to reply
  • 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