I need a list of unique pointers to ints (I need the underlying int arrays because of MPI compatibility, they are buffers that are getting tracked for work done). I have a workaround but would like to understand what is happening
std::list<std::unique_ptr<int[]>> const buf_send2 = std::list<std::unique_ptr<int[]>>{std::make_unique<int[]>(this->buffersize)}; Doesn't work as an initializer in the class because of
/usr/include/c++/15.2.1/bits/new_allocator.h:191:11: error: use of deleted function ? ‘std::unique_ptr<_Tp [], _Dp>::unique_ptr(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp = int; _Dp = std::default_delete<int []>]’ 191 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I know ahead of time how many buffers I need in this case (or specifically the maximum), so I can just use an array but this is exposing my weaknesses as a C++ programmer and wanted to try and understand what was happening.
This works fairly straightforwardly with non-array unique pointers can array unique pointers not be moved? Is there a way of doing this correctly?
std::vector<int>for the buffers and then use.data()method to pass to C API if needed ?std::initializer_list, butstd::unique_ptris non-copyable. Simple demo: godbolt.org/z/d8xfExoj3.std::unique_ptr<int>.list, then construct another list based upon the first? While the compiler will optimize away the second construction, the result is more typing, and is harder to read, than simplystd::list<std::unique_ptr<int[]>> const buf_send2{std::make_unique<int[]>(this->buffersize)};.