0

I have a type pod that I'd like to pass a handle around using a unique_ptr with custom deleter. Only it doesn't work because the move assignement operator is implicitly deleted. I don't understand exactly why? The deleter is a pod type which typically are nothrow move assignable, so it would meet the requirement specified on cppreference no?

Demo

#include <memory_resource> 
#include <memory> 
#include <optional>
#include <string_view>
#include <vector>
#include <cstdio>
 
using allocator_t = std::pmr::polymorphic_allocator<std::byte>;

struct pod
{
    int a;
    bool b;
};


template <typename T>
struct allocator_delete
{
    auto operator()(T* p) -> void {
        allocator_.delete_object(p);
    }
    allocator_t allocator_;
};

using pod_handle_t = std::unique_ptr<pod, allocator_delete<pod>>;


struct msg_outbound_metainfo_t
{
    pod_handle_t msg_handle_;
};

int main()
{
    pod_handle_t ptr;

    std::vector<msg_outbound_metainfo_t> vec;

    auto& meta = vec.emplace_back();
    meta.msg_handle_ = std::move(ptr);
}

Error

<source>:41:37: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = pod; _Dp = allocator_delete<pod>]'
   41 |     meta.msg_handle_ = std::move(ptr);
      |                                     ^
In file included from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/memory:76,
                 from <source>:2:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/unique_ptr.h:406:19: note: 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = pod; _Dp = allocator_delete<pod>]' is implicitly deleted because the default definition would be ill-formed:
  406 |       unique_ptr& operator=(unique_ptr&&) = default;
      |                   ^~~~~~~~
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/unique_ptr.h:406:19: error: use of deleted function 'std::__uniq_ptr_data<_Tp, _Dp, true, false>& std::__uniq_ptr_data<_Tp, _Dp, true, false>::operator=(std::__uniq_ptr_data<_Tp, _Dp, true, false>&&) [with _Tp = pod; _Dp = allocator_delete<pod>]'
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/unique_ptr.h:243:24: note: declared here
  243 |       __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
      |                        ^~~~~~~~
glades
  • 3,778
  • 1
  • 12
  • 34

0 Answers0