Search code examples
c++arraysc++11c++14smart-pointers

assignment to an array of std::unique_ptr


struct MyStruct
{
    int x = 0;
}

std::array<std::unique_ptr<MyStruct>, 10> Arr;

// Arr[0] = ?

What is the syntax to assign an object to such an array? My reference.


Solution

  • Answered by Fei Xiang:

    Arr[0].reset(new MyStruct);
    

    Answered by Remy Lebeau:

    Arr[0] = std::make_unique<MyStruct>(); // Since C++14