4

I read somewhere that the new standard force move semantics in the implementation of the string class. Right now compilers like gcc for example, implement the strings as copy on write to improve performance, this makes copying and passing string as parameters by value very cheap. Now if move semantics for copying strings are now mandatory, isnt that a performance lose?. Because if it is true that it will be cheap to pass strings moving them between scopes it will still need to do the copy if/when you copy the string, right?.

Can someone clarify this issue for me?

Thanks.

Chinji
  • 41
  • 4
  • I suppose 'copy on write' could just turn into 'move or copy on write'. Moving a string is so cheap though, any performance loss would be minimal I would think – David Nov 18 '13 at 02:14
  • Btw, COW implementations of `std::string` are no longer allowed in C++11. Gcc will hopefully fix this. [Source](http://stackoverflow.com/questions/12199710/legality-of-cow-stdstring-implementation-in-c11). – sbabbi Nov 18 '13 at 03:53

1 Answers1

5

Why you're counting move semantic a loss of performance? When it's correctly implemented, move a string is equivalent to copy several pointers only.

Well, for raw copying COWs are good, but they're bad for a multi-threaded environment and people're seeking ways to disable it.

http://www.gotw.ca/gotw/045.htm

Turning off COW in GCC

Is std::string refcounted in GCC 4.x / C++11?

At last, most string operations in a practical program are just passing it to other functions as is, so a const & is enough for performance.

Community
  • 1
  • 1
Xiangyan Sun
  • 453
  • 4
  • 11