// { dg-do run { target c++17 } } #include #include constexpr void test_convert_contained_value_to_bool() { struct False { constexpr operator bool() const { return false; } }; False f; std::optional o = f; // Should use optional(const optional&) ctor, not optional(U&&): std::optional o2 = o; // Contained value should be static_cast(f) not static_cast(o): VERIFY( o2.value() == false ); std::optional o3; std::optional o4 = o3; // Should have no contained value, not static_cast(o3): VERIFY( ! o4.has_value() ); } constexpr void test_convert_contained_value_to_bool_explicit() { struct False { constexpr explicit operator bool() const { return false; } }; False f; std::optional o = f; // Should use optional(const optional&) ctor, not optional(U&&): std::optional o2(o); // Contained value should be static_cast(f) not static_cast(o): VERIFY( o2.value() == false ); std::optional o3; std::optional o4(o3); // Should have no contained value, not static_cast(o3): VERIFY( ! o4.has_value() ); } int main() { test_convert_contained_value_to_bool(); test_convert_contained_value_to_bool_explicit(); #if __cpp_lib_optional >= 202106 static_assert([] { test_convert_contained_value_to_bool(); test_convert_contained_value_to_bool_explicit(); return true; }()); #endif }