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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:51:52+00:00 2026-05-23T00:51:52+00:00

If I have the following function declaration that returns a std::list<Triangle*> by value: std::list<Triangle*>

  • 0

If I have the following function declaration that returns a std::list<Triangle*> by value:

std::list<Triangle*> getAllAbove(Triangle* t);

when I return the std::list<Triangle*> (which is created on the stack in getAllAbove) at the end of getAllAbove, will GCC be able to optimize out the call to std::list<Triangle*>‘s copy constructor (which presumably would iterate through all of the elements and copy them), or at least copy only the list metadata (e.g. not the elements themselves)? The list could potentially contain a few thousand pointers and I would like to avoid copying all of them needlessly.

Is the only way in which to bypass the copy constructor call to create the list on the heap and then return a pointer to it?

  • 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-23T00:51:53+00:00Added an answer on May 23, 2026 at 12:51 am

    Well, the alternative way to bypass copying is to use the “return parameter” idiom instead of using the return value of the function

    void getAllAbove(Triangle* t, std::list<Triangle*>& result);
    

    Instead of forming the result “on the stack” as you do now, form it directly in result parameter (i.e. in the recipient list that you pass from the caller).

    As for your original code, whether the copying will take place or not depends on the capabilities of your compiler.

    From the most abstract point of view, your code actually has two copyings. (And yes, these are full-fledged copyings when the entire content of the list is meticulously duplicated in another list, element by element.) Firstly, the named list object you create on the stack inside your function is copied to a nameless temporary object that holds the return value of the function. Then the temporary object is copied to the final recipient object in the calling code. Most compilers will be able to eliminate one of these copyings (to eliminate the intermediate temporary, as allowed by C++98 specification).

    To eliminate the second one the compiler has to be able to perform so called Named Return Value Optimization (as allowed by C++03 specification). A compiler that supports Named Return Value Optimization should be able to essentially implicitly convert your function’s interface into the equivalent of the above “return parameter” idiom. I would expect GCC to be able to do it. Try it and see what happens.

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

Sidebar

Related Questions

I have the following function defined inside my linked list class. The declaration in
Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer,
I have a function with the following declaration: void cleanValid(int valid[][4], int &size, int
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have the following function that is pulling data from a database. The ajax
I have the following code which works as expected: #include <iostream> using namespace std;
I have a function that is exported by a C library with the following
I have some trouble forward declaring a function that uses boost::enable_if : the following
I have the following function: //Function to get random number public static int RandomNumber(int
I have the following function: - (NSString *)urlEncodedValue { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(

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.