Move or swap a stringstream

老子叫甜甜 提交于 2019-11-29 07:02:49

This is a missing feature on GCC : see bug 54316 , it has been fixed (you can thank Jonathan Wakely) for the next versions (gcc 5)

Clang with libc++ compiles this code :

int main () {

  std::stringstream stream("1234");

  std::stringstream stream2 = std::move(std::stringstream("5678"));

  return 0;
}

Live demo

And it also compiles the example with std::stringstream::swap

I have an alternative to moving or swapping, one can also clear and set a stringstream to a new string:

    #include <string>       // std::string
    #include <iostream>     // std::cout
    #include <sstream>      // std::stringstream

    int main () {
      std::stringstream ss("1234");

      ss.clear();
      ss.str("5678");

      int val;
      ss >> val; std::cout << "val: " << val << '\n';

      return 0;
    }

It's a clean work around that does not require one to refactor code, except for the localized section where the swap is changed to a clear() and str().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!