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

前端 未结 2 1668
天涯浪人
天涯浪人 2021-02-04 23:47

I was hoping stringstream has a constructor that steals its initial content from a string&&. Do such inter-species \"move constructors\" genera

2条回答
  •  抹茶落季
    2021-02-05 00:27

    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&& str,
       ios_base::openmode which = ios_base::out,
       const Allocator& a = Allocator());
    

    AND move str()

    template
    void str(basic_string&& s);
    

提交回复
热议问题