public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Tim Song <t.canens.cpp@gmail.com>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [committed] libstdc++: Add missing test for std::optional::transform(F&&)
Date: Wed, 20 Oct 2021 20:22:39 +0100	[thread overview]
Message-ID: <CACb0b4=gs=1UAi+qDE1rkz41VNj9_7mr1GZECaECBmiTbxe2aA@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4nBpiY9youtM40_TEHWQUjcqytomCQFWZC4hUTwSsm7cg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 732 bytes --]

On Wed, 20 Oct 2021 at 09:42, Jonathan Wakely wrote:
>
> On Wed, 20 Oct 2021 at 06:48, Tim Song wrote:
> >
> > On Tue, Oct 19, 2021 at 9:05 AM Jonathan Wakely via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> > >
> > > +constexpr bool
> > > +test_copy_elision()
> > > +{
> > > +  return true;
> > > +}
> > > +
> > > +static_assert( test_copy_elision() );
> > > +
> >
> > This isn't much of a test :)
>
> The ultimate copy elision. The copies even get removed from the source code.
>
> Thanks for spotting it. I meant to check we're constructing directly
> into the union member without materializing the temporary early. I'll
> add that today.

Fixed by the attached patch. Tested x86_64-linux, pushed to trunk.

Thanks again!

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1399 bytes --]

commit 4ba4b053151a20262d4b61eb4501aa1c48337abb
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Oct 20 20:12:28 2021

    libstdc++: Add missing test for std::optional::transform(F&&)
    
    The test_copy_elision() function was supposed to ensure that the result
    is constructed directly in the std::optional, without early temporary
    materialization. But I forgot to write the test.
    
    libstdc++-v3/ChangeLog:
    
            * testsuite/20_util/optional/monadic/transform.cc: Check that
            an rvalue result is not materialized too soon.

diff --git a/libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc b/libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc
index d01ccb2e0f2..13977b8ba8d 100644
--- a/libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc
+++ b/libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc
@@ -110,6 +110,23 @@ static_assert( test_forwarding() );
 constexpr bool
 test_copy_elision()
 {
+  struct immovable
+  {
+    constexpr immovable(int p) : power_level(p) { }
+    immovable(immovable&&) = delete;
+
+    int power_level;
+  };
+
+  struct Force
+  {
+    constexpr immovable operator()(int i) const { return {i+1}; }
+  };
+
+  std::optional<int> irresistible(9000);
+  std::optional<immovable> object = irresistible.transform(Force{});
+  VERIFY( object->power_level > 9000 );
+
   return true;
 }
 

      reply	other threads:[~2021-10-20 19:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-19 14:01 [committed] libstdc++: Implement monadic operations for std::optional (P0798R8) Jonathan Wakely
2021-10-20  5:47 ` Tim Song
2021-10-20  8:42   ` Jonathan Wakely
2021-10-20 19:22     ` Jonathan Wakely [this message]

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='CACb0b4=gs=1UAi+qDE1rkz41VNj9_7mr1GZECaECBmiTbxe2aA@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=t.canens.cpp@gmail.com \
    /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: link
Be 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).