unique_ptr

Please can someone help me this question. The answer is probably obvious but here I go...
Why is line one ok, and not line 2?

1
2
3
4
5
  std::unique_ptr<int[]> myPTR(new int [10] {}); // ok
  

  std::unique_ptr<int[][]> myArray(new int [10][10] {}); // not ok
A type like int[][] does not exists. As a parameter you can leave the inner most [] empty but any extra dimension requires the size:

std::unique_ptr<int[][10]> myArray(new int [10][10] {});
and though it's not part of the question, for what it's worth, for any non trivial type prefer std::make_unique to the new operator:
http://stackoverflow.com/questions/37514509/advantages-of-using-make-unique-over-new-operator
Using std::make_unique as suggested by gunnerfunner.
 
auto myArray = std::make_unique<int[][10]>(10);
Thanks very much everyone, you have been a great help.


James
Last edited on
Topic archived. No new replies allowed.