deduct.pass.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <optional>
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. // UNSUPPORTED: libcpp-no-deduction-guides
  12. // template<class T>
  13. // optional(T) -> optional<T>;
  14. #include <optional>
  15. #include <cassert>
  16. struct A {};
  17. int main()
  18. {
  19. // Test the explicit deduction guides
  20. {
  21. // optional(T)
  22. std::optional opt(5);
  23. static_assert(std::is_same_v<decltype(opt), std::optional<int>>, "");
  24. assert(static_cast<bool>(opt));
  25. assert(*opt == 5);
  26. }
  27. {
  28. // optional(T)
  29. std::optional opt(A{});
  30. static_assert(std::is_same_v<decltype(opt), std::optional<A>>, "");
  31. assert(static_cast<bool>(opt));
  32. }
  33. // Test the implicit deduction guides
  34. {
  35. // optional(const optional &);
  36. std::optional<char> source('A');
  37. std::optional opt(source);
  38. static_assert(std::is_same_v<decltype(opt), std::optional<std::optional<char>>>, "");
  39. assert(static_cast<bool>(opt) == static_cast<bool>(source));
  40. assert(*opt == *source);
  41. }
  42. }