C ++: moving semantics in # append line

I need to combine some lines into one, and for an effective reason, I want to use move semantics in this situation (and, of course, these lines will no longer be used). So I tried

#include <iostream> #include <algorithm> #include <string> int main() { std::string hello("Hello, "); std::string world("World!"); hello.append(std::move(world)); std::cout << hello << std::endl; std::cout << world << std::endl; return 0; } 

I assumed that he would deduce

 Hello, World! ## NOTHING ## 

But it actually outputs

 Hello, World! World! 

This will do the same if you replace append with operator+= . What is the right way to do this?

I am using g ++ 4.7.1 on debian 6.10

+4
source share
1 answer

You cannot move string to part of another string . This will require the new string effectively have two storage buffers: the current and the new. And then it would have to magically make it all contiguous, because C ++ 11 requires that std::string be in continuous memory.

In short, you cannot.

+7
source

Source: https://habr.com/ru/post/1443136/


All Articles