PragmaTwice commented on code in PR #768:
URL: https://github.com/apache/incubator-kvrocks/pull/768#discussion_r938439776


##########
tests/cppunit/status_test.cc:
##########
@@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <status.h>
+
+TEST(StatusOr, Scalar) {
+  auto f = [](int x) -> StatusOr<int> {
+    if (x > 10) {
+        return {Status::NotOK, "x large than 10"};
+    }
+
+    return 2 * x + 5;
+  };
+
+  ASSERT_EQ(*f(1), 7);
+  ASSERT_EQ(*f(5), 15);
+  ASSERT_EQ(f(7).GetValue(), 19);
+  ASSERT_EQ(f(7).GetCode(), Status::cOK);
+  ASSERT_EQ(f(7).Msg(), "ok");
+  ASSERT_TRUE(f(6));
+  ASSERT_EQ(f(11).GetCode(), Status::NotOK);
+  ASSERT_EQ(f(11).Msg(), "x large than 10");

Review Comment:
   If you look at the definition of `StatusOr<T>::GetValue`:
   
   ```c++
     value_type& GetValue() & {
       CHECK(*this);
       return getValue();
     }
   
     value_type&& GetValue() && {
       CHECK(*this);
       return std::move(getValue());
     }
   
     const value_type& GetValue() const& {
       CHECK(*this);
       return getValue();
     }
   ```
   
   You will find there is three different overload of this method via changing 
the qualifier of `*this`.
   
   Hence, in `f(x).GetValue()`, obviously `f(x)` is a 
[prvalue](https://en.cppreference.com/w/cpp/language/value_category#prvalue) 
since a temporary object is returned, and then the signature `value_type&& 
GetValue() &&` is decided to be called after the [overload 
resolution](https://en.cppreference.com/w/cpp/language/overload_resolution), 
and thus you get an 
[xvalue](https://en.cppreference.com/w/cpp/language/value_category#xvalue) 
(which is a special kind of rvalue) referenced to the underlying value of the 
returned `StatusOr`.
   
   So you should take care of the lifetime in this case, the rvalue will become 
invalid after the computation of the current expression. You can extend it via 
a call to a move ctor. This is not a defect of this type, but it is an idiom of 
C++, you should always care about lifetime of references to avoid dangling 
references.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to