简体   繁体   中英

No need to use const & for STL container parameters as we have move semantics?

Usually I use this to avoid copying cost:

void bar(const string& s);
void foo(const vector<int>& v);

Are STL containers in C++11 all support move semantics?

If so, does following code have the same performance as const & ?

void bar(string s);
void foo(vector<int> v);

Move semantics don't just magically make your code faster.

With them, calling a function like void bar(string s) is faster than it would be if you had to copy your arguments, but only in the case where the argument can be moved. Consider this case:

std::string prompt(std::string prompt_text);

void askOnce(std::string question) { prompt(question); }

void askManyTimes(std::string question) {
   for(int i=0; i<10; ++i) {
       askOnce(question);
   }
}

In the case of askOnce , the argument can be copied into the function or moved. When it's calling prompt, the argument can be moved.

In askManyTimes however, you need to keep the argument around so you can't move, so you actually end up having to create 10 copies of your question for no good reason.

Generally, if you don't need to modify your string or copy it somewhere else, you should still use const std::string& ; You can leave the reference off if you need to do a copy later on anyway.

Are STL containers in C++11 all support move semantics?

Yes.

If so, does following code have the same performance as const & ?

No, if the argument is an lvalue . If the argument is an rvalue , the performance is at least as good.

  • In the case of an lvalue , the argument has to be copied. There's no way around that. The function signature specifies that it doesn't modify its arguments, but a move operation might modify the object that is being moved from.

  • In the case of an rvalue , the argument can be moved from, if supported.

So if the parameter will be copied inside the function anyway, it is better to pass it by value, so that rvalues can be moved from, while lvalues will still be copied.

It depends what you pass in. If you pass a temporary object it will get moved in but if you pass a named variable it will get copied .

bar(func_returns_string()); // move

std::string s;
bar(s); // copy

You can force it to move a named variable using std::move :

bar(std::move(s)); // move (now s is empty)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM