I currently try to initialize the following array, Spot is a class that is defined elsewhere:
static const int WIDTH = 7; static const int HEIGHT = 6; std::array<std::array<std::unique_ptr<Spot>, WIDTH>, HEIGHT> field; When trying to initialize it:
for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { field.at(i).at(j) = std::make_unique<Spot>(new Spot()); } } it states that Spot* cannot be converted to const Spot &, which makes pretty much sense.
Google is not really helpful here, as the questions either deal with
std::unique_ptr<T> [] or std::uniqe_ptr<std::array<T>> but not with std::array<std::unique_ptr<T>> So, how do you achieve that? Is that even a thing? Or are you not supposed to use std::arrays with smart pointers at all?
std::arrayis irrelevant. You can use a plainunique_ptr<Spot>and have the same error.make_unique, it's about the difference between usingmake_uniqueand using the constructor ofunique_ptrdirectly. No, this one is not about using STL container and smart pointer together, even though you are the OP and you perceive your own problem this way. The linked question solves your problem because you are using anewexpression as the argument ofmake_unique.