make_optional.pass.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <optional>
  11. // template <class T>
  12. // constexpr optional<decay_t<T>> make_optional(T&& v);
  13. #include <optional>
  14. #include <string>
  15. #include <memory>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. int main()
  19. {
  20. using std::optional;
  21. using std::make_optional;
  22. {
  23. int arr[10]; ((void)arr);
  24. ASSERT_SAME_TYPE(decltype(make_optional(arr)), optional<int*>);
  25. }
  26. {
  27. constexpr auto opt = make_optional(2);
  28. ASSERT_SAME_TYPE(decltype(opt), const optional<int>);
  29. static_assert(opt.value() == 2);
  30. }
  31. {
  32. optional<int> opt = make_optional(2);
  33. assert(*opt == 2);
  34. }
  35. {
  36. std::string s("123");
  37. optional<std::string> opt = make_optional(s);
  38. assert(*opt == s);
  39. }
  40. {
  41. std::unique_ptr<int> s(new int(3));
  42. optional<std::unique_ptr<int>> opt = make_optional(std::move(s));
  43. assert(**opt == 3);
  44. assert(s == nullptr);
  45. }
  46. }