]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add missing test for std::optional::transform(F&&)
authorJonathan Wakely <jwakely@redhat.com>
Wed, 20 Oct 2021 19:12:28 +0000 (20:12 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 20 Oct 2021 19:20:18 +0000 (20:20 +0100)
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.

libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc

index d01ccb2e0f23cb7851cda20c935153ab5a6a1265..13977b8ba8d409292ef43b3b03419ebc95c86ff3 100644 (file)
@@ -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;
 }