0

I have function foo which get std::string as parameter:

void foo(std::string);

and I put a concatence string:

std::string str_1= "example_str_1";
std::string str_2 = "example_str_2";
foo(str_1+str_2);

It is a good way to use a concatence string with std::move?

foo(std::move(str_1+str_2));

Is there any difference between put concatence with std::move?

Cœur
  • 37,241
  • 25
  • 195
  • 267
M. S.
  • 109
  • 7

2 Answers2

7

No. The purpose of std::move is to cast to an rvalue, but str_1+str_2 is already an rvalue, making the move call redundant.

The larger room for improvement here is in the signature of foo – why does it take a std::string by value? If it only observes the data, the real win here would be changing foo to void foo(std::string const&), or use something akin to C++17's std::string_view.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Thank you for answer, yes of course with foo(std::string const&) – M. S. Jan 19 '17 at 12:48
  • 1
    @M.S. : You're welcome. If you need to research value categories further, there's a lot of good information in [this post](http://stackoverflow.com/q/3601602/636019). – ildjarn Jan 19 '17 at 12:49
2

There is no difference as std::string has a move constructor.

string (string&& str) noexcept;

Acquires the contents of str.
str is left in an unspecified but valid state.

Ref

Rama
  • 3,222
  • 2
  • 11
  • 26