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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:04:12+00:00 2026-06-07T04:04:12+00:00

For some reason, I want to return an object of my::Vector (which is basically

  • 0

For some reason, I want to return an object of my::Vector (which is basically a wrapper class that internally use STL vector for actual storage plus do provide some extra functions). I return vector by value as the function creates a vector locally every time.

my::Vector<int> calcOnCPU()
{
  my::Vector<int> v....
  return v;
} 

Now I can have multiple nesting of function calls (considering a library design), so in short something like following:

my::Vector<int> calc()
{
    if(...)
      return calcOnCPU();
}

AFAIK, returning by value would invoke copy constructor of my::Vector class which is something:

Vector<int>::Vector(const Vector& c)
{
   ....
   m_vec = c.m_vec; // where m_vec is std::vector<int>
}

Few questions:
1) In copy constructor, is it invoking copy constructor of std::vector? or assignment operator and Just to confirm, std::vector creates deep copy (meaning copies all elements considering basic integer type).
2) With nesting of calcOnCPU() in calc() each returning Vector of int: 2 or 1 copies of Vector will be created? How could I avoid multiple copies in case of such simple method nesting? Inline functions or there exist another way?

UPDATE 1: It became apparent to me that I need to keep my own copy constructor as there are some custom requirements. However, I did a simple test in main function:

int main() {
   ...
   my::Vector v = calc();
   std::cout<<v;
}

I put some prints using “std::cerr” in my copy constructor to see when it gets called. Interestingly, it is not called even once for above program (atleast nothing gets printed). Is it copy ellision optimization? I am using GNU C++ compiler (g++) v4.6.3 on Linux.

  • 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-07T04:04:13+00:00Added an answer on June 7, 2026 at 4:04 am

    In copy constructor, is it invoking copy constructor of std::vector? or assignment operator

    In your case, it’s creating an empty std::vector, then copy-assigning it. Using an initialiser list would copy-construct it directly, which is neater and possibly more efficient:

    Vector<int>::Vector(const Vector& c) : m_vec(c.m_vec) {
        ....
    }
    

    Just to confirm, std::vector creates deep copy

    Yes, copying a std::vector will allocate a new block of memory and copy all the elements into that.

    With nesting of calcOnCPU() in calc() each returning Vector of int: 2 or 1 copies of Vector will be created?

    That’s up to the compiler. It should apply the “return value optimisation” (a special case of copy elision), in which case it won’t create a local object and return a copy, but will create it directly in the space allocated for the returned object. There are some cases where this can’t be done – if you have multiple return statements that might return one of several local objects, for example.

    Also, a modern compiler will also support move semantics where, even if the copy can’t be elided, the vector’s contents will be moved to the returned object rather than copied; that is, they will be transferred quickly by setting the vector’s internal pointers, with no memory allocation or copying of elements. However, since you’re wrapping the vector in your own class, and you’ve declared a copy constructor, you’ll have to give that class a move constructor in order for that to work – which you can only do if you’re using C++11.

    How could I avoid multiple copies in case of such simple method nesting? Inline functions or there exist another way?

    Make sure the structure of your function is simple enough for copy elision to work. If you can, give your class a move constructor that moves the vector (or remove the copy constructor, and the assignment operator if there is one, to allow one to be implicitly generated). Inlining is unlikely to make a difference.

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

Sidebar

Related Questions

I want to use http://msdn.microsoft.com/en-us/library/aa370654%28VS.85%29.aspx in my code. But for some reason I cannot
I'm now developing an app on iOS. With some reason, I want to use
I have derived a class BitmapDrawable in android for some reason but I want
I have some object classes that use inheritance. It seems that I can only
For some reason I can't figure this out. But basically I want to compare
I want to call a method in java which blocks for some reason. I
for some reason I want to check how the deadlock occurred in web application
My server for some reason displays .AIR file inside the browser, but I want
I want to check if the day is Sunday, but for some reason I
I have a ASP.Net MVC JsonResult function in which I want to return the

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.