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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:46:52+00:00 2026-06-13T01:46:52+00:00

What is copy elision? What is (named) return value optimization? What do they imply?

  • 0

What is copy elision? What is (named) return value optimization? What do they imply?

In what situations can they occur? What are limitations?

  • If you were referenced to this question, you’re probably looking for the introduction.
  • For a technical overview, see the standard reference.
  • See common cases here.
  • 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-13T01:46:54+00:00Added an answer on June 13, 2026 at 1:46 am

    Introduction

    For a technical overview – skip to this answer.

    For common cases where copy elision occurs – skip to this answer.

    Copy elision is an optimization implemented by most compilers to prevent extra (potentially expensive) copies in certain situations. It makes returning by value or pass-by-value feasible in practice (restrictions apply).

    It’s the only form of optimization that elides (ha!) the as-if rule – copy elision can be applied even if copying/moving the object has side-effects.

    The following example taken from Wikipedia:

    struct C {
      C() {}
      C(const C&) { std::cout << "A copy was made.\n"; }
    };
     
    C f() {
      return C();
    }
     
    int main() {
      std::cout << "Hello World!\n";
      C obj = f();
    }
    

    Depending on the compiler & settings, the following outputs are all valid:

    Hello World!
    A copy was made.
    A copy was made.


    Hello World!
    A copy was made.


    Hello World!

    This also means fewer objects can be created, so you also can’t rely on a specific number of destructors being called. You shouldn’t have critical logic inside copy/move-constructors or destructors, as you can’t rely on them being called.

    If a call to a copy or move constructor is elided, that constructor must still exist and must be accessible. This ensures that copy elision does not allow copying objects which are not normally copyable, e.g. because they have a private or deleted copy/move constructor.

    C++17: As of C++17, Copy Elision is guaranteed when an object is returned directly, and in this case, the copy or move constructor need not be accessible or present:

    struct C {
      C() {}
      C(const C&) { std::cout << "A copy was made.\n"; }
    };
     
    C f() {
      return C(); //Definitely performs copy elision
    }
    C g() {
        C c;
        return c; //Maybe performs copy elision
    }
     
    int main() {
      std::cout << "Hello World!\n";
      C obj = f(); //Copy constructor isn't called
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's stated in [C++11: 12.8/31] : This elision of copy/move operations, called copy elision,
I ran across this article on copy ellision in C++ and I've seen comments
I copy a script of Kevin S (member of this site) like this <table
I copy-pasted from MSDN this code: using System.Security.Cryptography; byte[] buffer = enc.GetBytes(text); SHA1CryptoServiceProvider cryptoTransformSHA1
I copy and paste code from this URL for creating and reading/writing a proc
What does an r-value reference look like from a lower-level perspective. I just can't
Just copy this code from perl cook book 2nd ed pp. 796 it returns:
copy() [function.copy]: php_network_getaddresses: getaddrinfo failed: When I use copy(http://example.com/simple.jpg,$target); that error occur.
In the example below, if we ignore the mutex for a second, copy elision
I copy a file like this: require 'fileutils' FileUtils.copy(source_file, dest_file) I would like to

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.