make_unique.array.pass.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // UNSUPPORTED: c++98, c++03, c++11
  9. #include <memory>
  10. #include <string>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. // The only way to create an unique_ptr<T[]> is to default construct them.
  14. class foo {
  15. public:
  16. foo () : val_(3) {}
  17. int get () const { return val_; }
  18. private:
  19. int val_;
  20. };
  21. int main(int, char**)
  22. {
  23. {
  24. auto p1 = std::make_unique<int[]>(5);
  25. for ( int i = 0; i < 5; ++i )
  26. assert ( p1[i] == 0 );
  27. }
  28. {
  29. auto p2 = std::make_unique<std::string[]>(5);
  30. for ( int i = 0; i < 5; ++i )
  31. assert ( p2[i].size () == 0 );
  32. }
  33. {
  34. auto p3 = std::make_unique<foo[]>(7);
  35. for ( int i = 0; i < 7; ++i )
  36. assert ( p3[i].get () == 3 );
  37. }
  38. return 0;
  39. }