deduct.pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <optional>
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // UNSUPPORTED: clang-5, apple-clang-9
  11. // UNSUPPORTED: libcpp-no-deduction-guides
  12. // Clang 5 will generate bad implicit deduction guides
  13. // Specifically, for the copy constructor.
  14. // template<class T>
  15. // optional(T) -> optional<T>;
  16. #include <optional>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. struct A {};
  20. int main(int, char**)
  21. {
  22. // Test the explicit deduction guides
  23. {
  24. // optional(T)
  25. std::optional opt(5);
  26. static_assert(std::is_same_v<decltype(opt), std::optional<int>>, "");
  27. assert(static_cast<bool>(opt));
  28. assert(*opt == 5);
  29. }
  30. {
  31. // optional(T)
  32. std::optional opt(A{});
  33. static_assert(std::is_same_v<decltype(opt), std::optional<A>>, "");
  34. assert(static_cast<bool>(opt));
  35. }
  36. // Test the implicit deduction guides
  37. {
  38. // optional(optional);
  39. std::optional<char> source('A');
  40. std::optional opt(source);
  41. static_assert(std::is_same_v<decltype(opt), std::optional<char>>, "");
  42. assert(static_cast<bool>(opt) == static_cast<bool>(source));
  43. assert(*opt == *source);
  44. }
  45. return 0;
  46. }