I'm trying to inherit unique_ptr<T[]> just to add a constructor which allocates the array using malloc and sets a const size field.
#include <memory>
#include <functional>
using std::function;
using std::unique_ptr;
using std::byte;
template<class _Tp, class _Dp = function<void(_Tp*)>>
class unique_array_ptr : public unique_ptr<_Tp[], _Dp> {
public:
using unique_ptr<_Tp[], _Dp>::unique_ptr;
unique_array_ptr(const size_t size) : unique_ptr<_Tp[], _Dp>((_Tp*) malloc(size), free), size(size) {}
const size_t size;
};
void move() {
unique_ptr<byte> hello(new byte[5]);
unique_array_ptr<byte> test = std::move(hello);
}
The test assignment triggers this error:
<source>: In function 'void move()':
<source>:18:44: error: conversion from 'std::remove_reference<std::unique_ptr<std::byte>&>::type' {aka 'std::unique_ptr<std::byte>'} to non-scalar type 'unique_array_ptr<std::byte>' requested
18 | unique_array_ptr<byte> test = std::move(hello);
| ~~~~~~~~~^~~~~~~
Also before anyone says just use std::vector, I'm working with a C library so I need to be able to pass in buffers that it can free later.
I tried to inherit the constructors with using unique_ptr<_Tp[], _Dp>::unique_ptr;
but something isn't working.
Update:
I changed it to this but I still get no viable conversion.
unique_ptr<byte[], function<void(byte*)>> hello(new byte[5]);
unique_array_ptr<byte> test = std::move(hello);