unique_ptr

Apr 20, 2017 at 12:33pm
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
Apr 20, 2017 at 12:40pm
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] {});
Apr 20, 2017 at 1:08pm
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
Apr 20, 2017 at 1:13pm
Using std::make_unique as suggested by gunnerfunner.
 
auto myArray = std::make_unique<int[][10]>(10);
Apr 21, 2017 at 6:56am
Thanks very much everyone, you have been a great help.


James
Last edited on Apr 21, 2017 at 7:00am
Topic archived. No new replies allowed.