Commit a484d190 authored by Pranjal Raihan's avatar Pranjal Raihan Committed by Facebook GitHub Bot

Make SimpleObservable<T> default-constructible if T is default-constructible

Summary: This simplifies generic code.

Reviewed By: andriigrynenko

Differential Revision: D25164246

fbshipit-source-id: 4f42a2360f7ce32978360ddad63d1ca9512e6135
parent 2e214867
......@@ -21,6 +21,11 @@
namespace folly {
namespace observer {
template <typename T>
template <typename, typename>
SimpleObservable<T>::SimpleObservable()
: context_(std::make_shared<Context>(std::make_shared<T>())) {}
template <typename T>
SimpleObservable<T>::SimpleObservable(T value)
: SimpleObservable(std::make_shared<const T>(std::move(value))) {}
......
......@@ -27,6 +27,11 @@ namespace observer {
template <typename T>
class SimpleObservable {
public:
template <
typename U = T,
typename = std::enable_if_t<std::is_default_constructible<U>::value>>
SimpleObservable();
explicit SimpleObservable(T value);
explicit SimpleObservable(std::shared_ptr<const T> value);
......
......@@ -734,3 +734,14 @@ TEST(Observer, WithJitterNoEarlyRefresh) {
EXPECT_EQ(0, **laggingObserver);
EXPECT_EQ(42, **delta);
}
TEST(SimpleObservable, DefaultConstructible) {
struct Data {
int i = 42;
};
static_assert(std::is_default_constructible<Data>::value);
static_assert(std::is_default_constructible<SimpleObservable<Data>>::value);
SimpleObservable<Data> observable;
EXPECT_EQ((**observable.getObserver()).i, 42);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment