Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving unique_ptr in c++

Tags:

c++

unique-ptr

I'm designing a class that has to get ownership of a unique_ptr and do sth with it. Here is a minimized version of the code:

Chunk.h:

class Chunk {
public:
    Chunk(std::unique_ptr<unsigned char[]> contents);
    std::unique_ptr<char[]> contents;
};

Chunk.cpp:

Chunk::Chunk(std::unique_ptr<unsigned char[]> content): 
             contents(std::move(content)){ }

but it couldn't compiled though to this error:

no matching function for call to ‘std::unique_ptr<char []>::unique_ptr(std::remove_reference<std::unique_ptr<unsigned char []>&>::type)’
like image 656
hamed1soleimani Avatar asked Dec 14 '25 05:12

hamed1soleimani


1 Answers

You are using a <unsigned char[]> template as argument and <char[]> template as member.

They are not even considered as the same class at all by the compiler. Use exactly the same type if you plan to use template functions like std::move with it.

like image 112
Aracthor Avatar answered Dec 15 '25 18:12

Aracthor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!