deduct.pass.cpp 1.9 KB

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