How to return unique_ptr of an array created inside my function?

127 views Asked by At

I've tried various ways of writing my function, all of them failing with some kind of error.

First I tried a straight forward const auto type on my unique pointer (same result as const unique_ptr<char[]>):

export unique_ptr<char[]> mem_new_dup(const char* data, const size_t length)
{
    const auto mem = make_unique<char[]>(length);
    memcpy(mem.get(), data, length);
    return mem;  // error is on this line
}

// error:
//1>F:\gitrepos\ve2\ve2\utilities.ixx(10,1): error C2280: 'std::unique_ptr<char [],std::default_delete<char []>>::unique_ptr(const std::unique_ptr<char [],std::default_delete<char []>> &)': attempting to reference a deleted function
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2686): message : see declaration of 'std::unique_ptr<char [],std::default_delete<char []>>::unique_ptr'
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2686,5): message : 'std::unique_ptr<char [],std::default_delete<char []>>::unique_ptr(const std::unique_ptr<char [],std::default_delete<char []>> &)': function was explicitly deleted

Then I tried to remove the const from the unique pointer (even though it is constant):

export unique_ptr<char[]> mem_new_dup(const char* data, const size_t length)
{
    auto mem = make_unique<char[]>(length);
    memcpy(mem.get(), data, length);
    return mem;
}

// error
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2536,1): error C2070: '_Ty': illegal sizeof operand
//1>        with
//1>        [
//1>            _Ty=char []
//1>        ]
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2535): message : while compiling class template member function 'void std::default_delete<char []>::operator ()(_Ty (*)) noexcept const'
//1>        with
//1>        [
//1>            _Ty=char []
//1>        ]
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2647): message : see reference to function template instantiation 'void std::default_delete<char []>::operator ()(_Ty (*)) noexcept const' being compiled
//1>        with
//1>        [
//1>            _Ty=char []
//1>        ]
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2574): message : see reference to class template instantiation 'std::default_delete<char []>' being compiled
//1>F:\gitrepos\ve2\ve2\utilities.ixx(7): message : see reference to class template instantiation 'std::unique_ptr<char [],std::default_delete<char []>>' being compiled
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2536,25): error C2338: can't delete an incomplete type
//1>D:\a01\_work\2\s\binaries\x86ret\inc\memory(2537,1): warning C4156: deletion of an array expression without using the array form of 'delete'; array form substituted

So how can I correctly write my function?

1

There are 1 answers

0
Blindy On BEST ANSWER

Turns out that the problem was related to C++20 modules, I was doing an import std.core; above this code and for some reason it was importing the clang memory module instead of the MSVC module. Using #include <memory> as usual fixes the problem.