#include <folly/Memory.h>
#include <folly/Portability.h>
#include <folly/Unit.h>
+#include <folly/Utility.h>
#include <exception>
#include <stdexcept>
#include <type_traits>
*/
explicit Try(T&& v) : contains_(Contains::VALUE), value_(std::move(v)) {}
+ template <typename... Args>
+ explicit Try(in_place_t, Args&&... args) noexcept(
+ noexcept(::new (nullptr) T(std::declval<Args&&>()...)))
+ : contains_(Contains::VALUE), value_(std::forward<Args>(args)...) {}
+
/// Implicit conversion from Try<void> to Try<Unit>
template <class T2 = T>
/* implicit */
#include <glog/logging.h>
#include <folly/Memory.h>
+#include <folly/Traits.h>
#include <folly/portability/GTest.h>
using namespace folly;
-TEST(Try, basic) {
- class A {
- public:
- A(int x) : x_(x) {}
-
- int x() const {
- return x_;
- }
- private:
- int x_;
- };
+namespace {
+class A {
+ public:
+ explicit A(int x) : x_(x) {}
+
+ int x() const {
+ return x_;
+ }
+ private:
+ int x_;
+};
+}
+
+TEST(Try, basic) {
A a(5);
Try<A> t_a(std::move(a));
EXPECT_EQ(5, t_a.value().x());
}
+TEST(Try, in_place) {
+ Try<A> t_a(in_place, 5);
+
+ EXPECT_EQ(5, t_a.value().x());
+}
+
+TEST(Try, in_place_nested) {
+ Try<Try<A>> t_t_a(in_place, in_place, 5);
+
+ EXPECT_EQ(5, t_t_a.value().value().x());
+}
+
// Make sure we can copy Trys for copyable types
TEST(Try, copy) {
Try<int> t;