deduct.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // <array>
  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, class... U>
  15. // array(T, U...) -> array<T, 1 + sizeof...(U)>;
  16. //
  17. // Requires: (is_same_v<T, U> && ...) is true. Otherwise the program is ill-formed.
  18. #include <array>
  19. #include <cassert>
  20. #include <cstddef>
  21. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  22. // Disable the missing braces warning for this reason.
  23. #include "disable_missing_braces_warning.h"
  24. #include "test_macros.h"
  25. int main(int, char**)
  26. {
  27. // Test the explicit deduction guides
  28. {
  29. std::array arr{1,2,3}; // array(T, U...)
  30. static_assert(std::is_same_v<decltype(arr), std::array<int, 3>>, "");
  31. assert(arr[0] == 1);
  32. assert(arr[1] == 2);
  33. assert(arr[2] == 3);
  34. }
  35. {
  36. const long l1 = 42;
  37. std::array arr{1L, 4L, 9L, l1}; // array(T, U...)
  38. static_assert(std::is_same_v<decltype(arr)::value_type, long>, "");
  39. static_assert(arr.size() == 4, "");
  40. assert(arr[0] == 1);
  41. assert(arr[1] == 4);
  42. assert(arr[2] == 9);
  43. assert(arr[3] == l1);
  44. }
  45. // Test the implicit deduction guides
  46. {
  47. std::array<double, 2> source = {4.0, 5.0};
  48. std::array arr(source); // array(array)
  49. static_assert(std::is_same_v<decltype(arr), decltype(source)>, "");
  50. static_assert(std::is_same_v<decltype(arr), std::array<double, 2>>, "");
  51. assert(arr[0] == 4.0);
  52. assert(arr[1] == 5.0);
  53. }
  54. return 0;
  55. }