public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-6829] libstdc++: Fix std::spanstream move assignment [PR104032] Date: Sun, 23 Jan 2022 22:49:53 +0000 (GMT) [thread overview] Message-ID: <20220123224953.E96E33858419@sourceware.org> (raw) https://gcc.gnu.org/g:51631875a2fa0af62ebda7484ac48368e1805dff commit r12-6829-g51631875a2fa0af62ebda7484ac48368e1805dff Author: Jonathan Wakely <jwakely@redhat.com> Date: Sun Jan 23 21:55:57 2022 +0000 libstdc++: Fix std::spanstream move assignment [PR104032] libstdc++-v3/ChangeLog: PR libstdc++/104032 * include/std/spanstream (basic_spanbuf(basic_spanbuf&&)): Use mem-initializer for _M_buf. (basic_spanbuf::Operator=(basic_spanbuf&&)): Fix ill-formed member access. * testsuite/27_io/spanstream/2.cc: New test. Diff: --- libstdc++-v3/include/std/spanstream | 15 +++- libstdc++-v3/testsuite/27_io/spanstream/2.cc | 113 +++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/std/spanstream b/libstdc++-v3/include/std/spanstream index 240866ff26f..000bda52a1e 100644 --- a/libstdc++-v3/include/std/spanstream +++ b/libstdc++-v3/include/std/spanstream @@ -75,10 +75,17 @@ template<typename _CharT, typename _Traits = char_traits<_CharT>> basic_spanbuf(const basic_spanbuf&) = delete; - /// Move constructor. In this implementation `rhs` is left unchanged. + /** Move constructor. + * + * Transfers the buffer and pointers into the get and put areas from + * `__rhs` to `*this`. + * + * In this implementation `rhs` is left unchanged, + * but that is not guaranteed by the standard. + */ basic_spanbuf(basic_spanbuf&& __rhs) - : __streambuf_type(__rhs), _M_mode(__rhs._M_mode) - { span(__rhs._M_buf); } + : __streambuf_type(__rhs), _M_mode(__rhs._M_mode), _M_buf(__rhs._M_buf) + { } // [spanbuf.assign], assignment and swap basic_spanbuf& operator=(const basic_spanbuf&) = delete; @@ -86,7 +93,7 @@ template<typename _CharT, typename _Traits = char_traits<_CharT>> basic_spanbuf& operator=(basic_spanbuf&& __rhs) { - basic_spanbuf(std::move(__rhs))->swap(*this); + basic_spanbuf(std::move(__rhs)).swap(*this); return *this; } diff --git a/libstdc++-v3/testsuite/27_io/spanstream/2.cc b/libstdc++-v3/testsuite/27_io/spanstream/2.cc new file mode 100644 index 00000000000..a13a50b0dce --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/spanstream/2.cc @@ -0,0 +1,113 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include <spanstream> +#include <testsuite_hooks.h> + +using std::ispanstream; +using std::ospanstream; +using std::span; + +void +test_move() +{ + char c; + { + const char str[] = "chars"; + std::ispanstream a(str); + std::ispanstream b = std::move(a); + VERIFY( b.span().data() == str && b.span().size() == 6 ); + VERIFY( b >> c ); + VERIFY( c == 'c' ); + + a = std::move(b); + VERIFY( a.span().data() == str && a.span().size() == 6 ); + VERIFY( a >> c >> c ); + VERIFY( c == 'a' ); + } + + { + char buf[10] = {}; + std::ospanstream a(buf); + std::ospanstream b = std::move(a); + VERIFY( b << 'c' ); + VERIFY( buf[0] == 'c' ); + VERIFY( !std::char_traits<char>::compare(buf, "c", 2) ); + + a = std::move(b); + VERIFY( a << 'h' << 'a' << "rs" ); + VERIFY( !std::char_traits<char>::compare(buf, "chars", 6) ); + } + + { + char buf[10] = {}; + std::spanstream a(buf); + std::spanstream b = std::move(a); + VERIFY( b.span().empty() ); + VERIFY( b << 'c' ); + VERIFY( buf[0] == 'c' ); + VERIFY( !std::char_traits<char>::compare(buf, "c", 2) ); + VERIFY( b.span().data() == buf && b.span().size() == 1 ); + VERIFY( b >> c ); + VERIFY( c == 'c' ); + + a = std::move(b); + VERIFY( a.span().data() == buf && a.span().size() == 1 ); + VERIFY( a << 'h' << 'a' << "rs" ); + VERIFY( !std::char_traits<char>::compare(buf, "chars", 6) ); + VERIFY( a.span().data() == buf && a.span().size() == 5 ); + } +} + +void +test_swap() +{ + { + const char str1[] = "chars"; + const char str2[] = "STRING"; + std::ispanstream a(str1); + std::ispanstream b(str2); + a.swap(b); + VERIFY( a.span().data() == str2 && a.span().size() == 7 ); + VERIFY( b.span().data() == str1 && b.span().size() == 6 ); + char c; + VERIFY( a >> c ); + VERIFY( c == 'S' ); + VERIFY( b >> c ); + VERIFY( c == 'c' ); + + swap(a, b); + VERIFY( a.span().data() == str1 && a.span().size() == 6 ); + VERIFY( b.span().data() == str2 && b.span().size() == 7 ); + VERIFY( a >> c >> c ); + VERIFY( c == 'a' ); + VERIFY( b >> c >> c ); + VERIFY( c == 'R' ); + } + + { + char buf1[] = "xxxxxxxxxxxxxxx"; + char buf2[] = "xxxxxxxxxxxxxxx"; + std::ospanstream a(buf1); + std::ospanstream b(buf2); + a.swap(b); + VERIFY( a << "STR" ); + VERIFY( !std::char_traits<char>::compare(buf2, "STRx", 4) ); + VERIFY( b << 'c' << 'h' ); + VERIFY( !std::char_traits<char>::compare(buf1, "chx", 3) ); + + swap(a, b); + VERIFY( a.span().size() == 2 ); + VERIFY( b.span().size() == 3 ); + VERIFY( a << 'a' << "rs" ); + VERIFY( !std::char_traits<char>::compare(buf1, "charsx", 6) ); + VERIFY( b << "IN" << 'G' ); + VERIFY( !std::char_traits<char>::compare(buf2, "STRINGx", 7) ); + } +} + +int main() +{ + test_move(); + test_swap(); +}
reply other threads:[~2022-01-23 22:49 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220123224953.E96E33858419@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).