diff --git a/libs/optional/test/optional.cpp b/libs/optional/test/optional.cpp index 1ef8c6a6..abe89404 100644 --- a/libs/optional/test/optional.cpp +++ b/libs/optional/test/optional.cpp @@ -303,6 +303,41 @@ namespace testspr { is >> opt3; TESTSPR_ASSERT(!opt3); } + + + // get + TESTSPR_BOTH_ASSERT(sprout::get(opt1) == 1234); + { + auto opt3 = sprout::optional(1234); + TESTSPR_ASSERT(sprout::get(opt3) == 1234); + + opt3.get() = 12345; + TESTSPR_ASSERT(sprout::get(opt3) == 12345); + } + + // get_pointer + TESTSPR_BOTH_ASSERT(*sprout::get_pointer(opt1) == 1234); + { + auto opt3 = sprout::optional(1234); + TESTSPR_ASSERT(*sprout::get_pointer(opt3) == 1234); + + opt3.get() = 12345; + TESTSPR_ASSERT(*sprout::get_pointer(opt3) == 12345); + } + + // get_optional_value_or + TESTSPR_BOTH_ASSERT(sprout::get_optional_value_or(opt1, 12345) == 1234); + TESTSPR_BOTH_ASSERT(sprout::get_optional_value_or(opt2, 12345) == 12345); + { + auto opt3 = sprout::optional(1234); + int v = 12345; + TESTSPR_ASSERT(sprout::get_optional_value_or(opt3, v) == 1234); + } + { + auto opt3 = sprout::optional(); + int v = 12345; + TESTSPR_ASSERT(sprout::get_optional_value_or(opt3, v) == 12345); + } } } } // namespace testspr diff --git a/sprout/optional/optional.hpp b/sprout/optional/optional.hpp index 19c3b14e..764efe47 100644 --- a/sprout/optional/optional.hpp +++ b/sprout/optional/optional.hpp @@ -58,7 +58,7 @@ namespace sprout { template explicit SPROUT_CONSTEXPR optional(optional const& v) : init(v.is_initialized()) - , val(v.get()) + , val(v.is_initialized() ? v.get() : holder_type()) {} optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {