Does STL container transfer work?

I donโ€™t remember whether the STL container passes a copy of the container or just another alias. If I have several containers:

std::unordered_map<int,std::string> _hashStuff; std::vector<char> _characterStuff; 

And I want to pass these variables to functions, can I make such a function:

 void SomeClass::someFunction(std::vector<char> characterStuff); 

Or will it make a copy of unordered_map / vector ? I think I might need to use shared_ptr .

 void SomeClass::someFunction(std::shared_ptr<std::vector<char>> characterStuff); 
+7
source share
3 answers

It depends. If you pass in an lvalue when you enter your function (in practice, if you pass in what has a name, to which the address operator belongs and can be applied), the copy constructor of your class will be called.

 void foo(vector<char> v) { ... } int bar() { vector<char> myChars = { 'a', 'b', 'c' }; foo(myChars); // myChars gets COPIED } 

If you pass an rvalue (roughly speaking, something that does not have a name and to which the operator address cannot be applied), and the class has a move constructor, then the object will be moved (which don't beware, just like creating " pseudonym, but rather to transfer the guts of the object into a new skeleton, making the previous skeleton useless).

When calling foo() below, the result of make_vector() is the value of r. Therefore, the object returned by it is moved when entering foo() (i.e., the vector constructor of the move will be called):

 void foo(vector<char> v); { ... } vector<char> make_vector() { ... }; int bar() { foo(make_vector()); // myChars gets MOVED } 

Some STL classes have a move constructor, but do not have a copy constructor, because they are essentially not unique_ptr (for example, unique_ptr ). You will not get a copy of unique_ptr when you pass it to a function.

Even for classes that have a copy constructor, you can still force the move semantics to use the std::move function to change your argument from lvalue to rvalue, but again, which does not create an alias, it simply transfers ownership of the object on the function you are calling. This means that you cannot do anything with the original object, except reassigning it to another value or destroying it.

For example:

 void foo(vector<char> v) { ... } vector<char> make_vector() { ... }; int bar() { vector<char> myChars = { 'a', 'b', 'c' }; foo(move(myChars)); // myChars gets MOVED cout << myChars.size(); // ERROR! object myChars has been moved myChars = make_vector(); // OK, you can assign another vector to myChars } 

If you find that the whole subject of the lvalue and rvalue references and the movement of semantics are unclear, this is very clear. I personally found this tutorial quite useful:

http://thbecker.net/articles/rvalue_references/section_01.html

You can also find information at http://www.isocpp.org or on YouTube (watch seminars by Scott Meyers).

+7
source

Yes, it will copy the vector because you are passing by value. Passing by value always makes a copy or move (which can be undone under certain conditions, but not in your case). If you want to refer to the same vector inside the function as outside, you can simply pass it by reference. Change your function to:

 void SomeClass::someFunction(std::vector<char>& characterStuff); 

The type std::vector<char>& is a reference type, "reference to std::vector<char> ". The characterStuff name will act as an alias for the object referenced by _characterStuff .

+5
source

C ++ is value-based: when passing an object by value, you get independent copies. If you do not want to receive a copy, you can use the link or the const link, instead:

 void SomeClass::someFunction(std::vector<char>& changable) { ... } void SomeClass::otherFunction(std::vector<char> const& immutable) { ... } 

When the called function cannot change the argument, but you do not want to create a copy of the object, you need to go past const& . Normally, I would not use something like std::shared_ptr<T> . There is a use of this type, of course, so as not to prevent copying when the function is called.

+4
source

All Articles