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.
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:Yes, copying a
std::vectorwill allocate a new block of memory and copy all the elements into that.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.
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.