48

I was hoping stringstream has a constructor that steals its initial content from a string&&. Do such inter-species "move constructors" generally not exist in the STL? If not, why not?

Museful
  • 5,715
  • 2
  • 36
  • 53
  • 6
    in the same vein, why doesn't [`.str(string&&)`](https://en.cppreference.com/w/cpp/io/basic_stringstream/str) exist? – kmdreko Jun 25 '18 at 00:28

2 Answers2

55

There's history, which is disappointing. But also a future that looks bright.

When move semantics went into C++11, it was huge, controversial, and overwhelming. I wanted to be able to move strings into and out of stringstream. However the politics at the time demanded that the internal store did not have to be a basic_string<charT>. For example the internal store could be a vector. And there was no ability to control things with an allocator. In any event, the need was recognized in the C++11 time frame, but it was just a bridge too far.

Fortunately Peter Sommerlad has picked up the slack with P0408. This proposal adds the functionality you seek, hopefully for C++20, but that is not certain yet. It has successfully passed through the LEWG, and is on the LWG's desk right now. They did not get to it this month in Rapperswil, purely because of an overloaded schedule. I am hopeful that it will pass through the LWG, and the full committee vote. It certainly will have my vote.

Howard Hinnant
  • 179,402
  • 46
  • 391
  • 527
  • Even if the internal store was esoteric, perhaps the move constructors could have been specified as having either O(1) or O(n) cost, implementation defined? It’s of course an otherwise pointless what-if, but is that idea at least sound in retrospect? – Kuba hasn't forgotten Monica Jun 25 '18 at 13:26
  • 1
    Absolutely. I was just out of wind. There was this issue, issues with the numerics algorithms (which have also been fixed), and probably some others (oh yeah, the uninitialized algorithms). On the one hand I wish I could have "moved" everything. On the other hand, it is gratifying that people consider this feature important enough to put work into it themselves. – Howard Hinnant Jun 25 '18 at 14:03
  • I totally get being out of wind :) Thank you for your bit of history! – Kuba hasn't forgotten Monica Jun 25 '18 at 14:07
12

Why doesn't std::stringstream::stringstream(std::string&&) exist?

This is due to std::stringstream's internal buffer, rdbuf.

rdbuf, (type std::string_buf), doesn't support non-copy access as per the motivation in proposal, p0408r4:

... there is no non-copying access to the internal buffer of a basic_stringbuf which makes at least the obtaining of the output results from an ostringstream inefficient, because a copy is always made

However, there's already a plan to support std::string move in stringsteam's constructor:

explicit basic_ostringstream(
   basic_string<charT, traits, Allocator>&& str,
   ios_base::openmode which = ios_base::out,
   const Allocator& a = Allocator());

AND move str()

template<class SAlloc = Allocator>
void str(basic_string<charT, traits, SAlloc>&& s);
isanae
  • 3,007
  • 1
  • 19
  • 41
Joseph D.
  • 10,071
  • 3
  • 21
  • 54
  • @NicolBolas, let me update my post based on [p0408r1](http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0408r1.pdf). Thank you for your attention. – Joseph D. Jun 25 '18 at 01:35
  • 3
    It's [on revision 4 now](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0408r4.pdf). – Nicol Bolas Jun 25 '18 at 01:38